Row and Cloumn find in cell array

2 views (last 30 days)
krtl mhmt
krtl mhmt on 1 Jan 2021
Answered: Stephen23 on 1 Jan 2021
f={[4,5];[6,8];7; 6;[];9;[8,9];[ ];[ ];[ ]}
ans=find([f{:}] == 6)
% ans= 3 6
I try this;
[row,col]=find([f{:}] == 6)
%row =
1 1
%col =
3 6
but I don't want this, 6 in f cell;
row/col; 2,1 and 4,1
Can u help me for this?

Answers (2)

Image Analyst
Image Analyst on 1 Jan 2021
Well here's one way.
f={[4,5];
[6,8];
7;
6;
[];
9;
[8,9];
[ ];
[ ];
[ ]}
count = 1;
for k = 1 : length(f)
has6 = find(f{k} == 6);
if ~isempty(has6)
locations{count} = [k, has6];
count = count + 1;
end
end
celldisp(locations)
Not very compact but it is intuitive. Someone will probably give you a one-liner using cellfun().

Stephen23
Stephen23 on 1 Jan 2021
What you want is actually the cell index and the index of the cell content, not the "row and column" index as you wrote.
f = {[4,5];[6,8];7;6;[];9;[8,9];[];[];[]};
nml = cumsum(cellfun(@numel,f));
idx = find([f{:}]==6);
idr = 1+sum(nml<idx,1)
idr = 1×2
2 4
idc = idx(:)-nml(idr-1)
idc = 2×1
1 1

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!