How to find all elements in a matrix row containing a specific value?
23 views (last 30 days)
Show older comments
First of all, my apologies if this has been asked. I have checked, and I've seen somewhat similar questions, but still can't get my code to work.
Anyway, I have a matrix
Conn=[1 2;2 3;3 4;1 4;2 4]
I need to be able to find all of the elements in a row that contains a specific value -- in this case, 3. So rows 2 and 3 of the Conn matrix contain the value 3; I need all of the other elements in those rows (so 2 and 4). How would I go about coding this? I know it's just calling rows/columns from the matrix and probably using the find command, which should be simple for someone more experienced, but I can't get it to work.
Thanks, --Aaron
0 Comments
Accepted Answer
Guillaume
on 15 Nov 2014
Use the two output version of find (that is get the rows and columns), and use the rows as index:
[row, ~] = find(Conn == 3);
Conn(row, :)
2 Comments
Toby Green
on 12 Feb 2021
Guillaume I just want to say thank you!
7 years on your answer has helped me with my code for my uni dissertation, thanks so much!
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!