find index of cells containing other cells

3 次查看(过去 30 天)
Hello,
I have a cell A of a size 1x9 and each of the 9 cells contains a cell 1xNumber, where Number is different for each of the 9 cells. E.g A{1,5} contains 1x100 cell and e.g A{1,5}{1,44} contains a number: 0.842. Now, I want to find the position of a fixed number, let's say 0.842. I want to search within all the cells. I checked manually and the number 0.842 is A{1,5}{1,44}. So as an output I want matlab to give me the numbers 5 and 44.
I tried with:
index = find([A{:}] == 0.824);
I'm getting an error:
Undefined function 'eq' for input arguments of type 'cell'.
I guess it's because my cell A contains cells...
Any help appreciated,
Thank you

采纳的回答

Image Analyst
Image Analyst 2016-5-22
I don't know why you don't just avoid the problem in the first place. I don't see any reason why the data in the cells needs to also be cells. Why not use just regular numerical arrays? So the cells contain arrays of doubles instead of other cells?

更多回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2016-5-21
编辑:Azzi Abdelmalek 2016-5-21
A=arrayfun(@(x) randi(5,1,randi(10)),1:9,'un',0) % Example
m=3 % Looking for 3 in each cell
indices=cellfun(@(x) find(ismember(x,m)),A,'un',0)
  2 个评论
Dommal
Dommal 2016-5-21
编辑:Dommal 2016-5-21
Thank you for your answer, I still have problems though. I use:
indices=cellfun(@(x) find(ismember(x,R_max)),OUTPUT_GG,'un',0)
where OUTPUT_GG is my cell containing cells and R_max is the fixed value which position I want to find.
I get errors:
Error using cell/ismember>cellismemberR2012a (line 192) Input A of class cell and input B of class double must be cell arrays of strings, unless one is a string.
Error in cell/ismember (line 56) [varargout{1:max(1,nargout)}] = cellismemberR2012a(A,B);
Error in @(x)find(ismember(x,R_max))
Error in Untitled3 (line 72) indices=cellfun(@(x) find(ismember(x,R_max)),OUTPUT_GG,'un',0)
Azzi Abdelmalek
Azzi Abdelmalek 2016-5-21
Maybe I used a bad Example, try this
A=arrayfun(@(x) num2cell(randi(5,1,randi(10))),1:9,'un',0) % Example
m=3 % Looking for 3 in each cell
indices=cellfun(@(x) find(ismember([x{:}],m)),A,'un',0)

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2016-5-21
编辑:Andrei Bobrov 2016-5-21
A1 = cellfun(@(x)[x{:}]',A,'un',0);
A1(cellfun(@isempty,A1)) = nan;
m = cellfun(@numel,A);
ii = cumsum(m);
i1 = ii + 1;
z = zeros(ii(end),1);
z(ii - m + 1) = 1;
zz = cumsum(z);
y = ones(ii(end),1);
y(i1(1:end-1)) = 1 - m(1:end-1);
i2 = cumsum(y);
ind = [zz, i2];
A2 = cat(1,A1{:});
out = ind(A2 == 0.824,:);
  2 个评论
Dommal
Dommal 2016-5-21
Thank you for your answer! the code works but it gives me wrong numbers: 4,56. And it should be 5, 44. Cell A{1,4}{1,56} is an empty cell in my data set.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by