I have the folowing:
vector=[1 3 8 9];
matrix=[ 100 1 5 9 6; 100 10 13 3 8; 100 9 10 1 4; ];
% I want to search and replace the vector element with "0"in the matrix (i.e new matrix should be : Newmatrix=[ 100 0 5 0 6; 100 10 13 0 0; 100 0 10 0 4; ]; )
The script is:
Newmatrix=zeros(size(matrix));
for i=1:numel(matrix)
for j=1:length(vector)
valvect=vector(j);
if matrix(i)==valvect
Newmatrix(i)=0;
else
Newmatrix(i)=matrix(i);
end
end
end
The results is not the desired one but:
Newmatrix=100 1 5 0 6
100 10 13 3 8
100 0 10 1 4
So what I'm doing wrong?
Thank you

1 comentario

Mike Lynch
Mike Lynch el 24 de Nov. de 2020
The accepted answer or changem are cleaner and more compact, but for the code you wrote the addition of a "break" should fix the problem.
if matrix(i)==valvect
Newmatrix(i)=0;
break
else ...

Iniciar sesión para comentar.

 Respuesta aceptada

Youssef  Khmou
Youssef Khmou el 3 de Mzo. de 2013

0 votos

hi, try ;
F=matrix;
for i=1:length(vector)
F(F==vector(i))=0;
end

5 comentarios

Ionut  Anghel
Ionut Anghel el 3 de Mzo. de 2013
Thank you. Very efficient
BJ Anderson
BJ Anderson el 12 de Mzo. de 2019
See the comments below about changem, which is the most versatile and efficient tool for such problems.
In general, you should be avoiding loops in MATLAB, and changem is the thought-out implementation that does just that.
Another option that avoids a loop is 'ismember':
matrix(ismember(matrix,vector))=0;
The ismember function in this case returns a logical array the same size as matrix, with 1s wherever it finds a member of vector, and zero otherwise. It is implemented here as an indexing array, so now all elements in matrix where the above condition is true is set to 0--or whatever arbitrary value you need.
Stephen23
Stephen23 el 12 de Mzo. de 2019
"In general, you should be avoiding loops in MATLAB"
Why ?
Joel Bly
Joel Bly el 11 de Abr. de 2019
@BJAnderson how do I change the 1s that come from the ismember function back to the values they are referring to, without changing the zeros?
Walter Roberson
Walter Roberson el 12 de Abr. de 2019
If you use ismember() with two outputs, then the second output is the index at which the element in the first parameter appears in the second parameter. In places that the element does not occur, then the returned value will be 0 there. You can select just the valid indices by indexing the second output by the first output.
[is_it_there, idx] = ismember(A, B);
idx(is_it_there)

Iniciar sesión para comentar.

Más respuestas (4)

Metin Ozturk
Metin Ozturk el 1 de Ag. de 2018

1 voto

The more vectorized and easier way to do this could be as follows:
new_matrix = changem(matrix,zeros(length(vector),1),vector);

2 comentarios

BJ Anderson
BJ Anderson el 12 de Mzo. de 2019
YES YES YES. It looks like hardly anyone knows about changem...I see similar questions asked and typically the "solution" involves a loop. A loop in MATLAB is the first sign you're doing something wrong.
changem covers so many situations, and it is elegant and concise, not to mention avoids the risk of re-reassigning an element.
changem is almost always the right anwer. Let the world know!
Thanks Metin for helping to spread the word.
BJ Anderson
BJ Anderson el 12 de Mzo. de 2019
A quick update on changem:
Sadly, if one inspects the actual code within changem, it functions as a loop. While it is a handy one-liner, it does not have the time-savings of moving from a looped function to an matrix-operation function.

Iniciar sesión para comentar.

Bruno Luong
Bruno Luong el 12 de Mzo. de 2019

1 voto

F = matrix .* ~ismember(matrix,vector)
Walter Roberson
Walter Roberson el 3 de Mzo. de 2013

0 votos

Suppose you set Newmatrix to 0 because matrix matched vector(1). Now what happens when you go on to the next j to test if matrix matched vector(2) ?
Image Analyst
Image Analyst el 3 de Mzo. de 2013

0 votos

Try it this way:
newMatrix = matrix % Initialize
for k = 1 : length(vector)
newMatrix(matrix==vector(k)) = 0
end

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Preguntada:

el 3 de Mzo. de 2013

Comentada:

el 24 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by