Info
此问题已关闭。 请重新打开它进行编辑或回答。
Loops in Matlab
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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
0 个评论
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)&(...);
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!