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
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
採用された回答
Image Analyst
2015 年 3 月 9 日
If you're comparing floating point numbers, and not integers, you have to use a tolerance, as described in the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
And, even if they're integers, the line
if actTimeStamp(i) = zeroCrossingTime
is the same as
if actTimeStamp(i)
Why? Well you know if you type a=b, it will spit out the value of a to the command window, because "a" is the result/answer or "ans" as MATLAB calls it. And if zeroCrossingTime is zero, then the if will evaluate to false whereas if zeroCrossingTime is anything other than zero, the if will evaluate as true. Probably not what you want. You probably want
if abs(actTimeStamp(i) - zeroCrossingTime) < someToleranceValue
その他の回答 (2 件)
Jan
2015 年 3 月 9 日
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?
Michael Haderlein
2015 年 3 月 9 日
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 件のコメント
Guillaume
2015 年 3 月 9 日
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.
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!