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

1 view (last 30 days)
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.

Accepted Answer

ANKUR KUMAR
ANKUR KUMAR on 1 Oct 2018
Edited: ANKUR KUMAR on 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

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!