Need help reading from arrays and finding corresponding value

1 vue (au cours des 30 derniers jours)
barry conrad
barry conrad le 11 Déc 2018
Commenté : Bob Thompson le 11 Déc 2018
clear
tempData = csvread('C:\Users\');
%time
hr = 14;%12+pm
min = 42;
sec=12;
time = 3700;%example
%time = ((hr*3600)+(min*60)+sec);
if time==1,time<1839
n=1;
elseif 3678>time>1839
n=2;
else 5516>time>3678
n=3;
end
disp(tempData(n,3))
Hi im trying to use this code to read the time inputted by the person then to find the row it meets and read the 3rd column requried however with the method ive done its not working and it would be unefficent is there a faster way of doing this , thanks for the help.
piccc.png

Réponse acceptée

Saeid
Saeid le 11 Déc 2018
Hi, assuming A is your 47x5 matrix, replace your if part with this:
for i=2:size(A,1)
if time < A(i,1)
n=i-1;
break
end;
end;

Plus de réponses (1)

Bob Thompson
Bob Thompson le 11 Déc 2018
Modifié(e) : Bob Thompson le 11 Déc 2018
When combining multiple if conditions you want to use & (and) and | (or).
For your purpose though you can just find the minimum difference instead of using an if statement.
[value,index] = min(abs(tempdata(:,1)-time));
disp(tempdata(index,3))
  2 commentaires
Saeid
Saeid le 11 Déc 2018
Hi, this is beautifiul, but does not necessarily give the right interval (e.g. for time = 5500).
I would replace it with:
n = find((A(:,1)-time)> 0, 1)-1;
disp(A(n,3))
Bob Thompson
Bob Thompson le 11 Déc 2018
Mmm, you are right. I misinterpreted how the data was being presented, and what was being asked. The find will definitely work, it is also possible to use max instead of min, with a little logic indexing.
[value,index] = max(tempdata((tempdata(:,1)-time)<=0,1));

Connectez-vous pour commenter.

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by