How can I save the results of every loop in a for-loop with two counters

1 回表示 (過去 30 日間)
Ali Mania
Ali Mania 2013 年 7 月 9 日
Hi Matlab Masters, I have my Code like this:
a=logical ([1]);
b=logical ( [ 1 1 0
1 1 1
0 1 1]);
c=logical ( [ 1 0 0 0
0 1 1 0
0 1 1 1
0 0 1 1]);
d=logical ( [ 1 1 1
1 1 1
1 1 1]);
E={ a
b
c
d};
for i= 1:numel(E)
for j=1:numel(E{i}(:,1))
a=numel(find(E{i}(j,:)))<2
end
end
i use the For-loop to check out, if the number digit "1" in each row of E is smaller as 2. My problem is : With the for-loop I always receive the result "a" as the result of the last loop. like this:
a =
1
a =
0
a =
0
a =
0
a =
1
a =
0
a =
0
a =
0
a =
0
a =
0
a =
0
But I want to save the value of "a" in every loop (here 11 rows means 11 loops) in a vector. Like this:
a = [1
0
0
0
1
0
0
0
0
0
0]
Or clearly, I want to do these steps to E :
  1. If a cell of E contains only one elment (1 x 1) (here it is E(1) ), do nothing to it
  2. Else , check it out, if the number of the digit "1" in each row of each cell in E is smaller as 2 so that in the end I have a result like this:
a = [ 0
0
0
0
1
0
0
0
0
0
0]
thanks for your help in advance !

採用された回答

Andrei Bobrov
Andrei Bobrov 2013 年 7 月 9 日
a0 = cellfun(@(x)sum(x>0,2) < 2,E,'un',0);
a = cat(1,a0{:});
  2 件のコメント
Ali Mania
Ali Mania 2013 年 7 月 9 日
Awesome ! 1000 thanks ! Do you have any idea about the Handling of the 1x1 cell in E ?
Andrei Bobrov
Andrei Bobrov 2013 年 7 月 9 日
a = sum(E{1}>0,2) < 2

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by