Indexing a cell using a table

2 visualizzazioni (ultimi 30 giorni)
Koula Lysi
Koula Lysi il 21 Giu 2022
Commentato: Eric Sofen il 21 Giu 2022
A is 90x90 cell and each cell of A includes a 100x1 double. B is 90x90 logical with ones being the cells that I want to retrieve from A.
My goal is to create a 90x90 cell, namely C, containing the context of the cells of A (100x1 double) if the corresponding index in B is 1 and containing [] (0x0 double) if the coressponding index in B is 0. The code C=A(B) returns a cell array, but this is not what I ask for.
Simplified example:
A is a 3x3 cell with cell containing a 3x1 double
A = {[1;2;3] [4;5;6] [7;8;9] ; [10;11;12] [13;14;15] [16;17;18] ; [19;20;21] [22;23;24] [25;26;27]}
B is a 3x3 double with ones being the cells that I want to retrieve
B = [1 0 0;1 0 1; 1 1 1]
C is the 3x3 cell that I want to create
C = {[1;2;3] [] [] ; [10;11;12] [] [16;17;18] ; [19;20;21] [22;23;24] [25;26;27]}
OR
C = {[1;2;3] [22;23;24] [16;17;18] ; [10;11;12] [] [25;26;27] ; [19;20;21] [] []}

Risposta accettata

Karim
Karim il 21 Giu 2022
i gues you need to translate the logical matrix B into indexes, e.g. using find
% create A
A = {[1;2;3] [4;5;6] [7;8;9] ; [10;11;12] [13;14;15] [16;17;18] ; [19;20;21] [22;23;24] [25;26;27]};
% create the logical indexes
B = [1 0 0;1 0 1; 1 1 1];
% now create C
C = cell(size(A));
C( find(B) ) = A( find(B) );
C
C = 3×3 cell array
{3×1 double} {0×0 double} {0×0 double} {3×1 double} {0×0 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}
C{1}
ans = 3×1
1 2 3
C{2}
ans = 3×1
10 11 12
C{4}
ans = []
% check some values...
C_test = {[1;2;3] [] [] ; [10;11;12] [] [16;17;18] ; [19;20;21] [22;23;24] [25;26;27]}
C_test = 3×3 cell array
{3×1 double} {0×0 double} {0×0 double} {3×1 double} {0×0 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}
  1 Commento
Eric Sofen
Eric Sofen il 21 Giu 2022
There is no need to use find. Use logical indexing:
% create A
A = {[1;2;3] [4;5;6] [7;8;9] ; [10;11;12] [13;14;15] [16;17;18] ; [19;20;21] [22;23;24] [25;26;27]};
% create the logical indexes
B = logical([1 0 0;1 0 1; 1 1 1]);
% now create C
C = cell(size(A));
C(B) = A(B)
C = 3×3 cell array
{3×1 double} {0×0 double} {0×0 double} {3×1 double} {0×0 double} {3×1 double} {3×1 double} {3×1 double} {3×1 double}

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by