Info

This question is closed. Reopen it to edit or answer.

Loops in Matlab

2 views (last 30 days)
Lee
Lee on 13 Mar 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
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

Answers (2)

Oleg Komarov
Oleg Komarov on 13 Mar 2011
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 on 13 Mar 2011
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!