Problem mit einer Schleife < Matlab < Mathe-Software < Mathe < Vorhilfe
|
Status: |
(Frage) beantwortet | Datum: | 22:28 So 22.10.2006 | Autor: | ML1983 |
Guten Tag liebe Gemeinde...
ich habe -wie im Titel schon erwähnt- ein Problem mit einer Schleife...trotz Anweisung und Ausgabe der Laufvariable, die auch korrekt sind, werden Werte nicht ausgegeben und ich weiß nicht warum...
hier zunächst der Code (ist was länger aber zu Anfang kommt nur die Matrix, die ausgewertet werden soll):
A=[0,-42.66689,0.0116144,0.4,0
,0.5,-42.6149,0.003213778,0.4,0
,1,-42.56672,0.004886739,0.4,0
,1.5,-42.52579,0.01331381,0.4,0
,2,-42.49991,0.003060672,0.4,0
,2.5,-42.44491,0.01535232,0.4,0
,3,-42.55354,-0.005523789,0.4,0
,3.5,-42.37819,0.008306009,0.4,0
,4,-42.37319,0.007848462,0.4,0
,4.5,-42.39057,0.008479829,0.4,0
,5,-42.35714,0.00100136,0.4,0
,5.5,-42.3425,0.01175907,0.4,0
,6,-42.32407,0.007946589,0.4,0
,6.5,-42.2985,0.003479351,0.4,0
,7,-42.25632,0.01112132,0.4,0
,7.5,-42.23112,0.003480014,0.4,0
,8,-42.25945,0.00138909,0.4,0
,8.5,-42.21858,0.01020826,0.4,0
,9,-42.19358,0.0008152429,0.4,0
,9.5,-42.17814,0.001808296,0.4,0
,10,-42.1573,0.01501709,0.4,0
,10.5,-42.13606,-0.0005771357,0.4,0
,11,-42.12769,0.004957576,0.4,0
,11.5,-42.09071,0.01118223,0.4,0
,12,-42.09585,0.002612215,0.4,0
,12.5,-42.09646,-0.002481778,0.4,0
,13,-42.07061,0.001209552,0.4,0
,13.5,-42.07112,-0.006688528,0.4,0
,14,-42.04115,0.007559139,0.4,0
,14.5,-41.99103,0.01341026,0.4,0
,15,-41.99884,-0.002639801,0.4,0
,15.5,-41.99772,0.01980025,0.4,0
,16,-42.05286,0.0009226399,0.4,0
,16.5,-41.99161,-0.002962434,0.4,0
,17,-41.96047,0.01273509,0.4,0
,17.5,-41.92253,0.01033519,0.4,0
,18,-41.93449,-0.0002280447,0.4,0
,18.5,-41.91023,0.01514618,0.4,0
,19,-41.93589,0.009339793,0.4,0
,19.5,-41.9051,-0.003014197,0.4,0
,20,-41.8567,0.01344145,0.4,0
,20.5,-41.91336,0.006496677,0.4,0.2
,21,-42.10007,0.004929826,0.4,0.2
,21.5,-42.92337,0.02205907,0.4,0.2
,22,-49.17412,0.03741205,0.4,0.2
,22.5,-90.32589,0.1997303,0.4,0.2
,23,-100.4932,0.2567228,0.4,0.2
,23.5,-99.74922,0.2487101,0.4,0.2
,24,-96.25849,0.2349588,0.4,0.2
,24.5,-96.03462,0.2332869,0.4,0.2
,25,-95.04783,0.219747,0.4,0.2
,25.5,-93.82022,0.2204393,0.4,0.2
,26,-94.00724,0.2226887,0.4,0.2
,26.5,-93.82721,0.2152237,0.4,0.2
,27,-93.80922,0.2242447,0.4,0.2
,27.5,-92.94232,0.2094187,0.4,0.2
,28,-89.44608,0.1973999,0.4,0.2
,28.5,-90.96667,0.2087338,0.4,0.2
,29,-90.371,0.2115427,0.4,0.2
,29.5,-90.99217,0.2030995,0.4,0.2
,30,-89.90103,0.2081105,0.4,0.2
,30.5,-157.491,0.4732194,0.4,0.4
,31,-141.9543,0.4128212,0.4,0.4
,31.5,-139.0546,0.4039884,0.4,0.4
,32,-137.7787,0.401494,0.4,0.4
,32.5,-137.3035,0.3947394,0.4,0.4
,33,-137.4452,0.3996785,0.4,0.4
,33.5,-139.7967,0.4084956,0.4,0.4
,34,-137.1362,0.3935816,0.4,0.4
,34.5,-135.8168,0.383311,0.4,0.4
,35,-139.2441,0.4081461,0.4,0.4
,35.5,-138.7656,0.3946614,0.4,0.4
,36,-138.3564,0.3959169,0.4,0.4
,36.5,-139.9585,0.4055296,0.4,0.4
,37,-138.1863,0.3975118,0.4,0.4
,37.5,-137.3677,0.3999077,0.4,0.4
,38,-139.2952,0.4023859,0.4,0.4
,38.5,-139.8047,0.4002547,0.4,0.4
,39,-140.1365,0.4196135,0.4,0.4
,39.5,-136.4946,0.3937599,0.4,0.4
,40,-137.8157,0.3915439,0.4,0.4
,40.5,-205.1181,0.6708156,0.4,0.6
,41,-189.6376,0.6070337,0.4,0.6
,41.5,-186.4997,0.6001305,0.4,0.6
,42,-187.0666,0.6032737,0.4,0.6
,42.5,-188.0113,0.6008053,0.4,0.6
,43,-186.4575,0.5960401,0.4,0.6
,43.5,-189.4061,0.6125911,0.4,0.6
,44,-187.196,0.5998666,0.4,0.6
,44.5,-187.7453,0.6033911,0.4,0.6
,45,-185.2308,0.6008569,0.4,0.6
,45.5,-186.851,0.5915159,0.4,0.6
,46,-189.3563,0.6003194,0.4,0.6
,46.5,-184.9506,0.5963098,0.4,0.6
,47,-185.7514,0.5905785,0.4,0.6
,47.5,-187.6504,0.5994654,0.4,0.6
,48,-186.6297,0.6001178,0.4,0.6
,48.5,-186.2499,0.5946161,0.4,0.6
,49,-187.3688,0.6014849,0.4,0.6
,49.5,-185.1405,0.5940224,0.4,0.6
,50,-188.1969,0.6004236,0.4,0.6
,50.5,-249.3976,0.8461423,0.4,0.8
,51,-236.0485,0.7980494,0.4,0.8
,51.5,-237.5418,0.8045149,0.4,0.8
,52,-236.2456,0.7961118,0.4,0.8
,52.5,-233.7488,0.7956443,0.4,0.8
,53,-236.7426,0.7979581,0.4,0.8
,53.5,-237.5803,0.7990096,0.4,0.8
,54,-235.339,0.7971044,0.4,0.8
,54.5,-236.0908,0.7943548,0.4,0.8
,55,-237.2433,0.7969108,0.4,0.8
,55.5,-236.5855,0.8033537,0.4,0.8
,56,-236.6679,0.8018806,0.4,0.8
,56.5,-237.2983,0.7994424,0.4,0.8
,57,-235.7565,0.7930274,0.4,0.8
,57.5,-235.4969,0.7964202,0.4,0.8
,58,-237.4498,0.7979856,0.4,0.8
,58.5,-237.0804,0.8021399,0.4,0.8
,59,-236.9789,0.8051631,0.4,0.8
,59.5,-236.5836,0.7997536,0.4,0.8
,60,-236.8999,0.7982602,0.4,0.8
];
B= [A(:,2), A(:,3), A(:,5)];
erg = mittelw(B)
---------------------------------------------------------
function B = mittelw(A)
j = 1;
start = min(A(:,3));
stop = max(A(:,3));
for p = start:0.2:stop
disp(p);
I = find(A(:,3)== p);
B(j,:) = length(I)^-1*sum(A(I,:))
j = j+1;
end
----------------------------------
Ich kann mir nicht erklären, warum er die 0.6-Zeile auslässt. Eigentlich ist die Matrix noch länger und es werden willkürlich mehrere Werte in der Matrix nicht berechnet. Wenn aber jetzt hingeht und manuell noch ne Zeile anfügt, in der man fest p=0.6 vorgibt und die entsprechende Zeiel in B heraussucht, dann macht er es....kann mir da wer helfen?
Bedankt
Ich habe diese Frage in keinem Forum auf anderen Internetseiten gestellt.
|
|
|
|
Hallo,
es scheint wohl ein Fließkommaproblem zu sein. Zumindest scheint es zu funktionieren, wenn man die ersten drei Zeilen der for-Schleife durch folgende ersetzt:
for p = 10*start:2:10*stop
disp(p/10);
I = find(A(:,3)== p/10);
Dann wird zum Schleifenzähler immer ein Integer addiert.
Das Problem erstaunt mich angesichts folgende Aussage:
"With finite precision arithmetic two variables are rarely exactly the same. When using C or FORTRAN you should never compare two floating numbers to see if they are the same. Instead you should check to see if they are close. Matlab does not use integer arithmetic so if you check to see if two numbers are the same it automatically checks to see if the variables are close. If you were to use C or FORTRAN then that last example could get you into big trouble. but Matlab does the checking for you in case the numbers are just really close. "
(Quelle: Cyclismo.org)
Gruß
Martin
|
|
|
|
|
Status: |
(Mitteilung) Reaktion unnötig | Datum: | 18:39 Mo 23.10.2006 | Autor: | ML1983 |
Vielen Dank Martin...ein Kollege an der Uni gab mir heute auch diesen Tipp...habe dann eine Abweichungsregel anstelle des direkten Vergleichs eingebaut und es klappt auch....aber irgendwie find ich es noch wie vor seltsam...
|
|
|
|