Indexing a cell using a table

조회 수: 4 (최근 30일)
Koula Lysi
Koula Lysi 2022년 6월 21일
댓글: Eric Sofen 2022년 6월 21일
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] [] []}

채택된 답변

Karim
Karim 2022년 6월 21일
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
Eric Sofen
Eric Sofen 2022년 6월 21일
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}

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by