Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Loops in Matlab

조회 수: 1 (최근 30일)
Lee
Lee 2011년 3월 13일
마감: MATLAB Answer Bot 2021년 8월 20일
I have a dataset which contains a number in column one and a number in column two. I am trying to make a loop which iterates through each row to check to see if any of the rows contain a specific number, if so the Variable 'Access' = 1, else 'Access' = 2
This code is not working, any help would be very much appreciated.
i=length(Day2)
t=0
while (t < i)
t = t+1
if (Day2(t,:) == [50.3779, -4.1227])
Access=1;
else
Access=2;
end
end

답변 (2개)

Oleg Komarov
Oleg Komarov 2011년 3월 13일
Day2(t,:) == [50.3779, -4.1227]
How did you get those values? If you simply copy pasted the results from teh command window, then you probably missed the fact that the display truncates numbers.
try:
format long g
and then retrieve your values again.
I would go for smt like this:
idx = (Day2(:,1) > 50 - tol & Day2(:,1) < 50 + tol) &...
(Day2(:,2) > -4 - tol2 & Day2(:,2) < -4 + tol2)
So you just need to set the tolerances accordingly
Oleg

Matt Tearle
Matt Tearle 2011년 3월 13일
When you say dataset, I take it that you don't mean a dataset array. So:
x = (Day2(:,1)==50.3779)&(Day2(:,2)==-4.1227);
Access = 2-any(x);
But be careful with == with floating-point values. It might be better to do
x = (abs(Day2(:,1)-50.3779)<tol)&(...);

이 질문은 마감되었습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by