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

조회 수: 6 (최근 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.

채택된 답변

Image Analyst
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
Snehalatha
Snehalatha 2015년 3월 9일
Hey this is close to what i want. thank you for the idea. may be this might wor. I'll tell you. Once again thanks for the idea.
Snehalatha
Snehalatha 2015년 3월 9일
This worked. The problem was with comparing floating point numbers. Now it identifies and stops at the cell where there is a match.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
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?
  댓글 수: 1
Snehalatha
Snehalatha 2015년 3월 9일
편집: Snehalatha 2015년 3월 9일
Hi Thanks for telling me the break. I think i didn't notice it. But i removed and recheck it and it doesn't work. Also I ran the code line by line. The problem seems to be in the comparison. my actTimestamprow is always = 0 i.e no match.
Both are double values and I rounded them off to 4 digits. I am attaching an excel with just these 2 columns here so that you can have a look at the values. if u see row B1380 and A118247 match but somehow the comparison doesn't work. i removed the break to display an error msg now to see what happens. It runs through my array and shows the error msg.

댓글을 달려면 로그인하십시오.


Michael Haderlein
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
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
Snehalatha 2015년 3월 9일
@Michael: I corrected that. It's right in the code but when i was editing it here it must have got deleted but i have used "==" .
When i run the code it runs the line but somehow doesn't recognise the match.
@Guillaumne: this was the first thing i tried when my code didnt work yesterday but it doesn't seem to help. The code runs but doesn't recognise the match.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by