How to find all rows and columns that contain a specific value in MATLAB

43 views (last 30 days)
Hello
I have a 1000 x 1000 table consisting of 1s and 0s. I want to find every row and column that consists of at least one 1 and return the indexes of them all in an array (e.g. [1 5 7 62 299 430 827]). How do I go about doing this?
Thank you
  1 Comment
Lianne Abrahams
Lianne Abrahams on 14 Feb 2020
Using for loops, but I don't know exactly how. I have a similar problem; I wish to sum all elements for which the row index is equal to or greater than the column index.
Idk how to specify indexing on a conditional basis, up to now we have only been shown how give absolute index values like X(1,2)

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 14 Feb 2020
Try this:
A = randi([0 1], 10, 5); % Create Array 'A'
A([4 8],:) = zeros(2,5); % Set Rows #4 & #8 Arbitrarily To All 0
B = find(any(A,2));

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!