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

3 vues (au cours des 30 derniers jours)
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 ]

Réponse acceptée

Jan
Jan le 20 Avr 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

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by