How to merge and slice arrays of different sizes?
19 次查看(过去 30 天)
显示 更早的评论
Say I have one aray called `ydata` which has dimension 1x24000 and I want to slice a part of this array, say from index 2:3000 and merge it with another array we shall call `udata` which is itself 1x24000. How might I slice the bit I want from `ydata` and then merge it with `udata` if that makes sense?
I suppose what it boils down to is this:
- we have 2 arrays with the same dimension
- I want to slice a part from array 1 and merge it with array 2
Is this possible?
回答(2 个)
cdawg
2023-1-28
编辑:cdawg
2023-1-28
I'm not totally sure if you mean add it as a new row to the second array or append the first array to the end of the second.
If you want to just append part of it to the end you can do this:
ydata = [2 1 9 10 11];
udata = [1 2 3 4 5];
newArray = [udata ydata(2:4)] % append indices 2 through 4 to the end of udata
If you want to append part of ydata to a new row of udata, they need to have the same number of columns:
newArray = [udata(2:4); ydata(2:4)]
Hope this helps
0 个评论
Chris
2023-1-28
Perhaps you'd be better off with a cell array.
ydata = randi(9,1,8)
ydata = num2cell(ydata)
udata = num2cell(randi(9,1,8))
udata(2,2:6) = ydata(1,2:6)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!