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

2 views (last 30 days)
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 ]

Accepted Answer

Jan
Jan on 20 Apr 2022
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

More Answers (0)

Categories

Find more on Data Types in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!