フィルターのクリア

How to index cell Matrix with a logical matrix?

11 ビュー (過去 30 日間)
Diego Makasevicius Barbosa
Diego Makasevicius Barbosa 2015 年 11 月 26 日
コメント済み: Florian B 2022 年 2 月 24 日
Hello all, I tried to index a cell matrix with logical matrix, but it does not return me the correct output.
I want to:
Index: A ={'a','b','c','d'; 'a','b','c','d'; 'a','b','c','d'}
Matrix: B =[1,0,1,0; 0,1,1,0; 0,0,1,1]
Output: C =['a','','c',''; '','b','c',''; '','','c','d'];
I tried 'C=A(logical(B));' but without success.
Could someone help me, please?
Thank you!
  2 件のコメント
Guillaume
Guillaume 2015 年 11 月 26 日
Please note that
A =['a','b','c','d'; 'a','b','c','d'; 'a','b','c','d']
is not a cell array. It is a plain matrix of character and is the same as:
A = ['abcd';'abcd';'abcd'];
A cell array is:
A = {'a','b','c','d'; 'a','b','c','d'; 'a','b','c','d'}
Note the use of curly brackets. The distinction is important for your desired output since
C =['a','','c',''; '','b','c',''; '','','c','d'];
is the same as
C = ['ac'; 'bc'; 'cd'];
Diego Makasevicius Barbosa
Diego Makasevicius Barbosa 2015 年 11 月 26 日
Sorry Guillaume, my fault. Actually is exactly as you said (A ={'a','b','c','d'; 'a','b','c','d'; 'a','b','c','d'}). I only typed the question incorrectly.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2015 年 11 月 26 日
Assuming you indeed have a cell array (unlike your example):
A = {'a','b','c','d'; 'a','b','c','d'; 'a','b','c','d'}
B = logical([1,0,1,0; 0,1,1,0; 0,0,1,1]);
%option 1: create an empty cell array and use logical indexing to copy A
C = cell(size(A));
C(B) = A(B)
%option 2: copy A, and use logical indexing to replace unwanted elements
D = A;
D(~B) = {''}
Note that for all intent and purposes '' and [] are the same, even if matlab does not display them the same
isequal(C, D) %returns true
  5 件のコメント
Ahmad Suliman
Ahmad Suliman 2017 年 7 月 30 日
Guillaume, when I do: D(~B) = {''}, the MATLAB throws an error "Function 'subsindex' is not defined for values of class 'cell'." Could it be due to version? maybe 2016 does not support this way of indexing? Thanks,
Florian B
Florian B 2022 年 2 月 24 日
@Thorsten: Using option 2 does indeed give you an array with "empty" values (actually ' '). You can however just assign [ ] directly, which MATLAB interprets as deletion command.
%option 2: copy A, and use logical indexing to replace unwanted elements
D = A;
D(~B) = []; % this will delete all arguments where B(i) ~= true. -> before: {''}
This question - solving the problem for a 2D-cell array - may also help. A full alternative to the snippet above would be:
% copy A, and use logical indexing to replace unwanted {elements} with {''}
D = A;
D(~B) = {''};
% Identify empty cells
idx = cellfun(@isempty, D);
% Delete rows with empty cell
D(~any(idx)) = [];

サインインしてコメントする。

その他の回答 (1 件)

Thorsten
Thorsten 2015 年 11 月 26 日
編集済み: Thorsten 2015 年 11 月 26 日
Not exactly want you want, but close:
C = A;
C(B ~= 1) = ' '
  1 件のコメント
Diego Makasevicius Barbosa
Diego Makasevicius Barbosa 2015 年 11 月 26 日
Thorsen, The problem of this solution is that the output is the follow array:
C= {'a' 'b' 'c' 'c' 'c' 'd'}
And I need to know which is the remaining values for each row.
Thank you!!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by