sum logical 1's independently for each column

5 ビュー (過去 30 日間)
Michael Angeles
Michael Angeles 2022 年 2 月 11 日
回答済み: Michael Angeles 2022 年 2 月 14 日
I've attached a sample data from excel.
how can I create a "for loop" to sum each column and give me print out total for each column.
I also need to compare the "length" of the column to the sum of the logical 1's in that column then print out a pass or fail for each column tested.
example:
1 1 1 1
1 1 1 1
1 1 1 0
1 1 1 1
-----------sum
first column = 4 ==length(column) = pass
fourth column = 3 == lenght(column = fail

採用された回答

DGM
DGM 2022 年 2 月 12 日
編集済み: DGM 2022 年 2 月 13 日
If the inputs are integer-valued, just use all()
A = randi([0 1],5)
A = 5×5
0 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 0 0 1 0 0 0 0 1 1
isfull = all(A,1)
isfull = 1×5 logical array
0 0 0 1 0
% if you really need text output for some reason
flmap = {'fail','pass'};
fulllabels = flmap(isfull+1)
fulllabels = 1×5 cell array
{'fail'} {'fail'} {'fail'} {'pass'} {'fail'}
  1 件のコメント
Michael Angeles
Michael Angeles 2022 年 2 月 13 日
Thanks DGM, I will try this one if it fits the output that we are looking for.

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

その他の回答 (2 件)

David Hill
David Hill 2022 年 2 月 12 日
No loop is needed.
r=readmatrix('logicalDataCount.xlsx');
s=sum(r);
l=s==size(r,1);
  10 件のコメント
David Hill
David Hill 2022 年 2 月 13 日
The below code is completely flexible (does not depend on the size of r)
s=sum(r);
allPassed = (numPasses == size(r, 1));
You are really not understanding, the logical array above tells you every column that is passed (1) or failed (0). Converting the logical array to 'pass' or 'fail' is trivial.
Michael Angeles
Michael Angeles 2022 年 2 月 13 日
Hi David,
Thanks...Maybe I don't understand...The reason why I want each column to print out pass or fail as part of the output message is to inform the operator immediately that the column checked for a certain message passed or failed. This allows the operator to re-run the test.

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


Michael Angeles
Michael Angeles 2022 年 2 月 14 日
Thank you Everyone!!

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by