Indexing a cell using a table

4 views (last 30 days)
Koula Lysi
Koula Lysi on 21 Jun 2022
Commented: Eric Sofen on 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] [] []}

Accepted Answer

Karim
Karim on 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 = 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 Comment
Eric Sofen
Eric Sofen on 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)
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}

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays 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!