Matlab Vlookup/Indexing equivalent
46 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alex Wiest
el 14 de Sept. de 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 comentarios
Stephen23
el 14 de Sept. de 2022
Editada: Stephen23
el 14 de Sept. de 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:
Respuesta aceptada
Stephen23
el 14 de Sept. de 2022
Editada: Stephen23
el 14 de Sept. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!