# Making each element of a row vector equal to zero

7 visualizaciones (últimos 30 días)
Aftab Ahmed Khan el 27 de Mayo de 2014
Comentada: Aftab Ahmed Khan el 28 de Mayo de 2014
Hi Everyone, I have a row vector (size 1*100) which contains randomly distributed 1s and 0s. How can i make each element of this row vector equal to zero using for loop ?
Sorry, if this is a very basic question !!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

James Kristoff el 27 de Mayo de 2014
There are many ways to modify arrays in MATLAB. First, let's look at the for loop method:
given:
% this could be any size row vector
foo = [0,1,0,0,1,1,0];
% loop through all elements of foo
for( i = 1:length(foo) )
% set each element to 0
foo(i) = 0;
end
Alternatively you could use logical indexing, e.g.
% this could be any size row vector
foo = [0,1,0,0,1,1,0];
% set foo equal to zero where foo equals 1
foo(foo == 1) = 0;
Also, in this simple case, you could just create a new row vector of all zeros:
% this could be any size row vector
foo = [0,1,0,0,1,1,0];
foo = zeros(size(foo));
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
James Kristoff el 27 de Mayo de 2014
There are also several options when dealing with Matrices. First, let me explain why you are seeing 1 1 1 when you execute the commands you mentioned.
% creates a 4x4 matrix
BS_channeltable = [1 1 0 1; 0 0 1 1; 1 0 1 0; 0 1 0 1];
let's split up this command to understand it better:
find(BS_channeltable(1,:))~= 0;
working from the innermost expression we have:
BS_channeltable(1,:)
which returns the first row (a 1x4 row vector) of the BS_channeltable matrix:
[1, 1, 0, 1]
then, that value is sent to the find function.
find([1, 1, 0, 1])
which returns the indices of any non-zero elements specifically:
[1, 2, 4]
then you are comparing these indices with 0 using not-equals
[1, 2, 4] ~= 0
and since none of these indices are zero, you get an array of true values, represented as:
[1, 1, 1]
This means that you could either use the indices returned by the find function directly i.e.
% get the indices of any non-zero values
indices = find(BS_channeltable(1,:));
% replace these specific values with 0
BS_channeltable(1, indices) = 0
Alternatively you could use logical indexing to get around using the find function i.e.
% set the any values in the first row of BS_channeltable
% that are not equal to zero, to zero
BS_channeltable(1, BS_channeltable(1,:) ~= 0) = 0;
Aftab Ahmed Khan el 28 de Mayo de 2014
Thank you so much, Done.

Iniciar sesión para comentar.

### Más respuestas (2)

James Tursa el 27 de Mayo de 2014
Editada: James Tursa el 27 de Mayo de 2014
You don't need a for loop. You can just do this:
row_vector(:) = 0; % Set all elements to 0, keep original variable type the same
##### 2 comentariosMostrar NingunoOcultar Ninguno
Aftab Ahmed Khan el 27 de Mayo de 2014
Hi, thank you for the reply. But i am trying to achieve something else. I have to use a for loop as i am using a "find function" to first find the location of 1 in the matrix and then replace it with zero. let say this is my matrix, BS_channeltable = [1 1 0 1; 0 0 1 1; 1 0 1 0; 0 1 0 1];
but when i use this command, find(BS_channeltable(1,:))~= 0; it answers me 1 1 1, but i want to find their vertices in the matrix.
I hope i have explained it well for you.
James Tursa el 27 de Mayo de 2014
Editada: James Tursa el 27 de Mayo de 2014
Not clear yet what you want. Are you trying to do this operation for only certain rows of a 2D matrix? If so, you can still do this without find and a for loop. E.g.,
BS_channeltable(1,:) = 0;
Is there some reason you need the indexes of these locations, other than to set their locations equal to 0?

Iniciar sesión para comentar.

George Papazafeiropoulos el 27 de Mayo de 2014
You can create a new vector with all zeros by typing the command:
new=zeros(1,100)
or by using a for loop:
for I=1:100
if v(I)==1
v(I)=0;
end
end
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Creating and Concatenating Matrices 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