sum logical 1's independently for each column

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

 Accepted Answer

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 Comment

Thanks DGM, I will try this one if it fits the output that we are looking for.

Sign in to comment.

More Answers (2)

No loop is needed.
r=readmatrix('logicalDataCount.xlsx');
s=sum(r);
l=s==size(r,1);

10 Comments

I need to print out a pass or fail for each column.
That is the logical array l.
l is probably a bad name because it looks too much like l or 1. Let's call it numPasses.
numPasses = sum(r);
allPassed = (numPasses == size(r, 1)); % Or better use DGM's answer with all(r, 1)
% Print out strings for pass or fail.
for k = 1 : length(numPasses)
if allPassed(k)
fprintf('Pass ')
else
fprintf('Fail')
end
end
% Print line feed after it's all done.
fprintf('\n');
Is there a way that I could specify the column number for example:
prints out ---> Column 1 Column2 Column3 Column4
Pass Pass Pass Fail
Thank you...just a beginner in matlab
Yes.
numPasses = sum(r);
allPassed = (numPasses == size(r, 1)); % Or better use DGM's answer with all(r, 1)
% Print out strings for pass or fail.
% First print out a column meader line.
fprintf('Column 1 Column2 Column3 Column4 \n');
for k = 1 : length(numPasses)
if allPassed(k)
fprintf('Pass ')
else
fprintf('Fail')
end
end
% Print line feed after it's all done.
fprintf('\n');
it would work but my data row and columns grow increase depending on the data size that I get. I want the function to automatically adjust as the data grows for example
prints out ---> Column 1 Column2 Column3 Column4
Pass Pass Pass Fail
new data ......
Column 1 Column2 Column3 Column4 Column 5 Column6 Column7 Column8
Pass Pass Pass Fail Pass Pass Fail Pass
Do you have multiple files to read then?
l=cell(1,numOfFiles);
for k=1:numOfFiles
r=readmatrix(strcat('logicalDataCount',num2str(k),'.xlsx');%I have no idea what all your files are named
s=sum(r);
l{k}=size(r,1);%you will need a cell array since the sizes are different
end
David, the file size is determined at the beginning how large the data is going to be. It is passed on by another software to my code with certain matrix size (m-rows x n-columns). I wanted the code to be flexible enough to read each (rows and columns) column of data and determine if it passed or not.
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.
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.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!