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
이전 댓글 표시
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.
채택된 답변
추가 답변 (2개)
Jan
2015년 3월 9일
1 개 추천
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
Snehalatha
2015년 3월 9일
편집: Snehalatha
2015년 3월 9일
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
Snehalatha
2015년 3월 9일
Michael Haderlein
2015년 3월 9일
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
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.
Snehalatha
2015년 3월 9일
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!