Indexing a cell using a table
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Koula Lysi
am 21 Jun. 2022
Kommentiert: Eric Sofen
am 21 Jun. 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] [] []}
0 Kommentare
Akzeptierte Antwort
Karim
am 21 Jun. 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{1}
C{2}
C{4}
% check some values...
C_test = {[1;2;3] [] [] ; [10;11;12] [] [16;17;18] ; [19;20;21] [22;23;24] [25;26;27]}
1 Kommentar
Eric Sofen
am 21 Jun. 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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!