How do I create a new matrix based on elements from a previous matrix?
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
ANKUR KUMAR
2018-10-1
编辑:ANKUR KUMAR
2018-10-1
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!