I am comparing a value to an array and trying to find out the match. But it is not working though there is a match
Mostra commenti meno recenti
function maximumValue = maximumvalue(voltageValues,actTimeStamp, timeStamp,thresholdRowValue)
actTimestamprow = 0;
maximumValue=0;
n=0;
zeroCrossingTime=0;
cycle=0;
analysisArray = [0];
thresholdRowValue = thresholdRowValue - 1;
if mod(thresholdRowValue,2) == 0
cycle = -1;
else
cycle = 1;
end
zeroCrossingTime = timeStamp(thresholdRowValue);
for i = 1:size(actTimeStamp)
if actTimeStamp(i) = zeroCrossingTime
actTimestamprow = i
else
break;
end
end
In this I have a row which matches the value. But the variable actTimestamprow displays zero.
the threshold value is a value passed from another function ( works fine). the actTimeStamp, volatgeValues and timestamp are arrays. Can you tell me why. Any help would be appreciated.
Risposta accettata
Più risposte (2)
Jan
il 9 Mar 2015
1 voto
Obviously your assumption, that you have "a row which matches the value" is wrong. Perhaps this is the typical floating point problem?
Please format you code properly and provide some input values, such that the problem can be reproduced. Be more precise, because "I have a row" is obvious for you, but the readers do not know, which variable you are talking of.
Are you sure you want to break the "for i" loop, when the first element is not matching?
Did you use the debugger to step through your code line by line, such that you can inspect the values of the variables?
1 Commento
Snehalatha
il 9 Mar 2015
Modificato: Snehalatha
il 9 Mar 2015
Michael Haderlein
il 9 Mar 2015
for i = 1:size(actTimeStamp)
size returns the size of both dimensions. I guess actTimeStamp is something like [1 x N], let's say N=10 (doesn't matter here). In this case, size(actTimeStamp] = [1 10]. If you use this as upper limit of the loop iterator, the first value of the array will be the maximum:
for cnt=1:[4 50]
end
>> cnt
cnt =
4
You need to use either size(actTimeStamp,2) to address the second dimension (10 in the upper example) or something like length() or numel().
4 Commenti
Snehalatha
il 9 Mar 2015
Michael Haderlein
il 9 Mar 2015
Ok, different point: are you sure your code even runs? The line
if actTimeStamp(i) = zeroCrossingTime
should immediately produce an error as the equality-check operator is ==, not =.
Guillaume
il 9 Mar 2015
Most people (and probably you as well) don't know the behaviour of the colon operator when given non-scalar inputs.
Save yourself some future trouble and be explicit on the dimension you need, particularly as it's only 2 more characters to write:
for i = 1:size(actTimeStamp, 1)
Your code (this part at least) work for now. In the future you may change your input and this'll be the source of a hard to find bug.
Snehalatha
il 9 Mar 2015
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!