change Index of non-zero values in 3D array

2 ビュー (過去 30 日間)
lucksBi
lucksBi 2017 年 3 月 11 日
コメント済み: lucksBi 2017 年 3 月 12 日
HI.. I have a 3D array like this:
val(:,:,1) =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 22 0 0 1
0 0 0 0 0
33 0 -1 0 0
0 0 0 -1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
The problem is that i want all values to be in same row. e.g. in val(:,:,1) it should be 33,22,-1,-1,1 in 6th row of matrix. Is there any way to do this? I am new to matlab so sorry if my question sounds awkward.

採用された回答

the cyclist
the cyclist 2017 年 3 月 11 日
編集済み: the cyclist 2017 年 3 月 11 日
Here is one way.
val(:,:,1) = [
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 22 0 0 1
0 0 0 0 0
33 0 -1 0 0
0 0 0 -1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0];
val(:,:,2) = [
0 0 0 0 0
14 0 0 0 0
0 0 4 0 0
0 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 16 0 0 0
0 0 0 2 0
0 0 0 0 0];
newval = zeros(size(val));
newval(6,:,:) = reshape(val(val~=0),size(val(1,:,:)));
I made up some 3D data based on your first "slice". Then, I create a newval matrix that is the same size (but all zeros), and fill in the 6th row with the non-zero values from val.
  1 件のコメント
lucksBi
lucksBi 2017 年 3 月 12 日
Thanks alot

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by