Indexing values of an array based on values of one column
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Elina Park
il 3 Ott 2018
Commentato: Elina Park
il 3 Ott 2018
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 Commenti
Risposta accettata
Image Analyst
il 3 Ott 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 Commenti
Image Analyst
il 3 Ott 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!