Vectorised cell search and logical indexing

Hello, I would like to know if there is a vectorised/cellfun/better approach to the loop below:
% Find Key 4 in block_new{:}(1,1)
Key4 = '=x';
index4 = cellfun(@(x)regexp(x, Key4), block_new, 'UniformOutput', false);
Logic = false(length(index4),1);
for i = 1 : length(index4)
Logic(i) = ~isempty(cell2mat(index4{i}(1,1)));
end
index4 = Logic;
block_set = block_new(:,index4);
Where the cells in the 1 X m cell array, block_new are in themselves cell arrays of various sizes.
I have already tried the following:
find(~cellfun(@isempty,index4{:}(1,1)))
Thanks in advance.

댓글 수: 3

Jos (10584)
Jos (10584) 2017년 9월 21일
what does block_new contain? Is it a cell array of strings?
Yes. A typical example, let's say block_new{i} would contain:
block_new{i}(1,1) -> String
block_new{i}(1,2:n} -> String of comma delimited doubles
~isempty(cell2mat(index4{i}(1,1)));
If the output of cell2mat is empty, the cell must have been empty already. What about:
~isempty(index4{i}{1})
instead?
Please post a relevant input array.

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

답변 (1개)

Jos (10584)
Jos (10584) 2017년 9월 21일

0 개 추천

regex can work on a cell array of strings directly:
block_new = {'abcd=x','abcd','a=xbc=x'}
index4 = regexp('block_new','=x')

댓글 수: 3

Apparently they are not all strings..
regexp(block_new,'=x')
Error using regexp
All cells must be char row vectors.
Jos (10584)
Jos (10584) 2017년 9월 21일
Some cells are cell array of strings themselves? Kind of complicated data structure ...
Cedric
Cedric 2017년 9월 21일
편집: Cedric 2017년 9월 21일
As mentioned by Jan, it would be simpler if you would attach a sample data set. Also, if you could describe in words what you are trying to achieve, that could help because you are doing a lot of operations that seem unnecessary (but that may in fact be necessary depending the final purpose).

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2017년 9월 21일

편집:

2017년 9월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by