Indexing values of an array based on values of one column
6 views (last 30 days)
Show older comments
I'm looking to index a value in a certain row of a matrix, based on the values in one of the columns. For example, if I have an array like this:
[45 1 234 457
58 1 283 384
49 2 384 221
92 2 983 174
97 1 937 123]
How can I index the values in column 3 for values where the value in column 2 of the same row is 1? Without specifying the exact position of the value like column 3, row 1 and such. Is there a way to use logical indexing to do this?
0 Comments
Accepted Answer
Image Analyst
on 3 Oct 2018
Sounds like homework. Is it?
I don't think you can use logical indexing, at least immediately, because they are different lengths -- col 2 has 5 values while row 1 has 4 values. Try using intersect() or ismember() to find the values in common, then use logical indexing. Write back if you need help.
2 Comments
Image Analyst
on 3 Oct 2018
OK, here's a further hint to get you started:
m = [45 1 234 457
58 1 283 384
49 2 384 221
92 2 983 174
97 1 937 123]
col2 = m(:, 2)
row1 = m(1, :)
matches = intersect(col2, row1)
[ia, ib] = ismember(col2, row1)
[ia, ib] = ismember(row1, col2)
See if you can take it from there.
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!