Find the values in the cell arrays that correspond to some specific indices
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Koula Lysi
el 20 de Abr. de 2022
Comentada: Koula Lysi
el 20 de Abr. de 2022
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 ]
0 comentarios
Respuesta aceptada
Jan
el 20 de Abr. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!