Size of each cell

1 次查看(过去 30 天)
Yro
Yro 2020-12-27
回答: Jan 2020-12-27
I need to know the size of each cell based on the following code:
ARRAY = randi(20,20,2);
for i = 1:length(ARRAY)
find_fcn1{i,:} = find(ARRAY(:,1)==(ARRAY(i,1) + 1));
find_fcn2{i,:} = find(ARRAY(:,2)==(ARRAY(i,2) + 1));
end
if lenght('of any cell in find_fcn1' >= 3) || lenght('of any cell in find_fcn2' >= 3)
'do something'
else
'do something'
end
I tried using cellfun but it doesn't work because I generate another array.
Thanks in advance.

回答(2 个)

Matt J
Matt J 2020-12-27
G1=findgroups(ARRAY(:,1));
G2=findgroups(ARRAY(:,2));
H1=histcounts(G1,1:max(G1)+1);
H2=histcounts(G2,1:max(G2)+1);
if any(H1>=3) || any(H2>=3)
%do something
else
%do something else
end

Jan
Jan 2020-12-27
"doesn't work" does not allow to understand, what the problem is. Please post the code you have tried and explain, what is not working as expected.
ARRAY = randi(20,20,2);
for i = 1:length(ARRAY)
find_fcn1{i,:} = find(ARRAY(:,1)==(ARRAY(i,1) + 1));
find_fcn2{i,:} = find(ARRAY(:,2)==(ARRAY(i,2) + 1));
end
ge3_fcn1 = cellfun(find_fcn1, 'length') >= 3;
ge3_fcn2 = cellfun(find_fcn2, 'length') >= 3;
match = find(ge3_fcn1 & ge3_fcn2)
Then maybe you need a for loop, not an if condition.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by