How to merge and slice arrays of different sizes?

15 ビュー (過去 30 日間)
HC98
HC98 2023 年 1 月 28 日
回答済み: Chris 2023 年 1 月 28 日
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:
  1. we have 2 arrays with the same dimension
  2. I want to slice a part from array 1 and merge it with array 2
Is this possible?
  3 件のコメント
HC98
HC98 2023 年 1 月 28 日
Slice and merge with the second array, i.e., add it on
Chris
Chris 2023 年 1 月 28 日
編集済み: Chris 2023 年 1 月 28 日
Are they double arrays? String arrays? chars? Do you want to add a second row to the array containing that slice?
u = randi(10,1,5);
y = randi(10,1,5);
u(2,2:4) = y(1,2:4)
u = 2×5
8 1 2 7 5 0 10 8 9 0

サインインしてコメントする。

回答 (2 件)

cdawg
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
newArray = 1×8
1 2 3 4 5 1 9 10
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)]
newArray = 2×3
2 3 4 1 9 10
Hope this helps

Chris
Chris 2023 年 1 月 28 日
Perhaps you'd be better off with a cell array.
ydata = randi(9,1,8)
ydata = 1×8
7 7 8 4 1 9 1 3
ydata = num2cell(ydata)
ydata = 1×8 cell array
{[7]} {[7]} {[8]} {[4]} {[1]} {[9]} {[1]} {[3]}
udata = num2cell(randi(9,1,8))
udata = 1×8 cell array
{[7]} {[2]} {[7]} {[2]} {[1]} {[3]} {[8]} {[7]}
udata(2,2:6) = ydata(1,2:6)
udata = 2×8 cell array
{[ 7]} {[2]} {[7]} {[2]} {[1]} {[3]} {[ 8]} {[ 7]} {0×0 double} {[7]} {[8]} {[4]} {[1]} {[9]} {0×0 double} {0×0 double}

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by