Find the values in the cell arrays that correspond to some specific indices

1 回表示 (過去 30 日間)
Koula Lysi
Koula Lysi 2022 年 4 月 20 日
コメント済み: Koula Lysi 2022 年 4 月 20 日
I have a cell array of size 99x90 (named A). Each cell includes either a scalar, either an array, either a NaN. I have created a matrix of size 99x90 (named B), with each cell including the index of the value that I would like to isolate from the corresponding cell in A. If the cell in A includes a scalar then the corresponding cell in B includes the scalar 1, if the cell in A includes an array then the corresponding cell in B includes a scalar no larger than the length of the array and if the cell in A includes a NaN then the corresponding cell in B includes a NaN. I would like to create a matrix of size 99x90 (named C) with the values that correspond to the indices in matrix B. Any idea how to do this?
SIMPLE EXAMPLE:
A = { [10 30 2] [2] [NaN] ; [4] [NaN] [NaN] ; [30 2] [10 2] [3] }
B = [ 2 1 NaN ; 1 NaN NaN ; 2 1 1 ]
C = [ 30 2 NaN ; 4 NaN NaN ; 2 10 3 ]

採用された回答

Jan
Jan 2022 年 4 月 20 日
If I understand correctly, A and B are the existing input and you want to create C. Then:
A = { [10 30 2] [2] [NaN] ; [4] [NaN] [NaN] ; [30 2] [10 2] [3] };
B = [ 2 1 NaN ; 1 NaN NaN ; 2 1 1 ];
C = NaN(size(A));
for k = 1:numel(A)
if ~isnan(B(k))
C(k) = A{k}(B(k));
end
end
C
C = 3×3
30 2 NaN 4 NaN NaN 2 10 3

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by