How to extract the values from a matrix 282x1 if I know the rows number?

1 visualización (últimos 30 días)
Flavia Aluisi
Flavia Aluisi el 28 de Mayo de 2017
Editada: Guillaume el 28 de Mayo de 2017
Dear all, I have the following problem: starting from a matrix A (282x1) I extract, with the command 'find', all the rows included in a given rage of values. So I got a second matrix B with all the rows numbers. Now my question is, how I can get a third matrix C with the Values of A corresponding to the rows in B?

Respuestas (2)

KSSV
KSSV el 28 de Mayo de 2017
Find gives you indices of the range of values you sought in A. B is your result from A. To get C, use
C=A(B);

Guillaume
Guillaume el 28 de Mayo de 2017
Editada: Guillaume el 28 de Mayo de 2017
If you have the rows indices, then it's just a matter of simple indexing. To extract the row numbers contained in the variable B, then for any arbitrary matrix it's simply:
C = A(B, :);
If your A has only one column, as in your case, then you don't even need the colon, so:
C = A(B);
However, if your B is the result of find, then you don't even need to use find nor generate B. You can use logical indexing instead. e.g.:
%you had for example:
%B = find(A == 5)
%C = A(B, :)
%instead:
C = A(A == 5, :) %no need for find
Using logical indexing will be (negligibly for arrays of your size) faster than using find + normal indexing.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by