Matlab Vlookup/Indexing equivalent
45 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Alex Wiest
am 14 Sep. 2022
Bearbeitet: Stephen23
am 14 Sep. 2022
I have a datetime array "t" that is 78888x1 in dimension like this, that is every hour for a period of time
'01-Jan-2027 00:00:00'
'01-Jan-2027 01:00:00'
'01-Jan-2027 02:00:00'
'01-Jan-2027 03:00:00'
'01-Jan-2027 04:00:00'
I also have a 9x4 matrix "input" that has yearly data, like this
2027 1.2 200 350
2028 3.7 200 350
2029 7.2 200 350
2030 7.8 200 350
2031 9.3 200 350
2032 9.3 200 350
2033 9.3 200 350
2034 10.9 200 350
2035 15.5 200 350
What i am trying to do is for every row in array "t", look up the year and index it with the year in column 1 from matrix "input" and then take the corresponding value from column 2,3 and 4 and copy into a new array called "output".
I am a matlab newbie, and am at a loss on where to start/how to go about this.
Any help is appreciated!
3 Kommentare
Stephen23
am 14 Sep. 2022
Bearbeitet: Stephen23
am 14 Sep. 2022
"I am completely at a loss on how to proceed as there is no “lookup” function in matlab"
The general solution is called indexing.
Indexing is simpler, yet more powerful and much more versatile than any Excel function:
Indexing is one of the important/fundamental topics that are introduced here:
But for your situation, you should probably use tables and one of the JOIN operators:
Akzeptierte Antwort
Stephen23
am 14 Sep. 2022
Bearbeitet: Stephen23
am 14 Sep. 2022
DT = [datetime(2027,1,1,(0:4).',0,0);datetime(2028,1,1,0,0,0)]
Y = year(DT);
M = [2027,1.2,200,350;2028,3.7,200,350;2029,7.2,200,350;2030,7.8,200,350;2031,9.3,200,350;2032,9.3,200,350;2033,9.3,200,350;2034,10.9,200,350;2035,15.5,200,350]
Approach one: ISMEMBER and indexing:
[idx,idy] = ismember(Y,M(:,1));
out = M(idy(idx),2:end)
Approach two: tables and JOIN:
T1 = table(DT,Y)
T2 = array2table(M, 'VariableNames',{'Y','a','b','c'})
T3 = join(T1,T2)
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!