Shift elements in a 3D array

1 回表示 (過去 30 日間)
Vaishnavi Sree Jeganathan
Vaishnavi Sree Jeganathan 2022 年 12 月 3 日
回答済み: Voss 2022 年 12 月 3 日
Hi,
I have a 3D array. I would like to remove few elements (say, I want to remove all 120s in the 3D array). Once I remove them, I would like to add zeros at the end to maintain the same vector length.
Could anyone please suggest an option to do this?
Thank you so much

回答 (1 件)

Voss
Voss 2022 年 12 月 3 日
Here's an example with a 2x3x2 array, removing all the 9s and appending 0s:
data = reshape(1:12,[2 3 2])
data =
data(:,:,1) = 1 3 5 2 4 6 data(:,:,2) = 7 9 11 8 10 12
siz = size(data);
idx = data == 9;
data(idx) = [];
data(end+1:end+nnz(idx)) = 0;
data = reshape(data,siz)
data =
data(:,:,1) = 1 3 5 2 4 6 data(:,:,2) = 7 10 12 8 11 0
Notice the 10, 11, 12 are now in different locations. Is that what you want?

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by