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