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

1 vue (au cours des 30 derniers jours)
Allie
Allie le 1 Oct 2018
Commenté : Allie le 1 Oct 2018
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.

Réponse acceptée

ANKUR KUMAR
ANKUR KUMAR le 1 Oct 2018
Modifié(e) : ANKUR KUMAR le 1 Oct 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

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by