Indexing a cell using a table
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
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{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
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!