How do I create a new matrix based on elements from a previous matrix?

1 visualización (últimos 30 días)
I have a matrix with columns depth and year. I have a vector, depth2, that is basically depth but with a few random rows missing. I would like an output that shows me what years correspond to the depths present in depth2.
depth=[1 2 3 4 5 6 7 8]
year=[2007 1960 1915 1871 1828 1785 1735 1680]
depth2=[3 4 5 7 8]
ideally year2 would be [1915 1871 1828 1735 1680]
The full depth/year matrix is size 149:2.

Respuesta aceptada

ANKUR KUMAR
ANKUR KUMAR el 1 de Oct. de 2018
Editada: ANKUR KUMAR el 1 de Oct. de 2018
depth=[1 2 3 4 5 6 7 8]
year=[2007 1960 1915 1871 1828 1785 1735 1680]
depth2=[3 4 5 7 8]
year2=arrayfun(@(x) year(depth==x),depth2)
If the previous prog seems to be difficult for you, then you can use simply loop.
depth=[1 2 3 4 5 6 7 8]
year=[2007 1960 1915 1871 1828 1785 1735 1680]
depth2=[3 4 5 7 8]
for i =1:length(depth2)
index=find(depth==depth2(i));
year2(i)=year(index)
end

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices 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!

Translated by