How to get the columns of non zero matrix?

1 view (last 30 days)
I have a matrix named A
a= [1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0 0 0;
0 0 1 0 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 1 0 0 0 0;
0 0 0 0 0 0 1 1 0 0;
0 0 0 0 0 0 0 0 1 1];
How can I have the number of column of non-zero number in a matrix? (Check by rows)
The result would be
Column = [1 0 2 3 4 5 6 7 8 9 1 0]
  1 Comment
Adam Danz
Adam Danz on 3 Dec 2018
I don't understand the quesiton. Is the output merely 1,2,3,4,5,6,7,8,9,10? Are you just cumulatively counting the number of 1s by row?
What would be the result of this input?
a = [ 1 0 1 1;
0 0 0 1;
0 0 0 0;
1 1 1 1];

Sign in to comment.

Accepted Answer

YT
YT on 3 Dec 2018
Edited: YT on 3 Dec 2018
Your example is a bit vague, but if the main question is to find the non-zero values in the matrix, I would suggest using the following
[r c] = find(a~=0) %returns rows (r) and columns (c) of non-zero values

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!