Need help reading from arrays and finding corresponding value
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
채택된 답변
Saeid
2018년 12월 11일
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;
댓글 수: 0
추가 답변 (1개)
Bob Thompson
2018년 12월 11일
편집: Bob Thompson
2018년 12월 11일
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
Saeid
2018년 12월 11일
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
2018년 12월 11일
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));
참고 항목
카테고리
Help Center 및 File Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!