Function to extract certain elements from a matrix

1 visualización (últimos 30 días)
Sebastian Ciuban
Sebastian Ciuban el 10 de Abr. de 2014
Comentada: Mischa Kim el 10 de Abr. de 2014
I have difficulties in making a function to extract columns after first number in the row from a the matrix. For example I have this matrix (A) and I want all columns that have first element 1 to be extracted in another matrix
A= 1 2 1 1 3
2 5 6 4 8
2 3 5 7 8
And I want my function to return this:
B= 1 1 1
2 6 4
2 5 7

Respuesta aceptada

Mischa Kim
Mischa Kim el 10 de Abr. de 2014
Ciuban, use
B = A(:,A(1,:)==1)
  3 comentarios
Sebastian Ciuban
Sebastian Ciuban el 10 de Abr. de 2014
It is possible to make a function, for example if I write extract(4) and in returns me all columns that have first element 4?
Mischa Kim
Mischa Kim el 10 de Abr. de 2014
Sure
function B = extr(A,extrnum)
B = A(:,A(1,:)==extrnum);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by