Borrar filtros
Borrar filtros

Make first and last element of array 0

19 visualizaciones (últimos 30 días)
Sarah Gomez
Sarah Gomez el 28 de Feb. de 2022
Comentada: Stephen23 el 21 de Mzo. de 2022
N = 12*0.5;
TAU_max = 15;
for i = 1:N
TAU(i,:) = i*(TAU_max/N);
end
I have a little loop here that creates a column vector with my dummy variables. But how I do make it so the (1,1) and (1:N) elements are 0 while still preserving my original values?
  1 comentario
Stephen23
Stephen23 el 21 de Mzo. de 2022
You don't need a loop:
N = 12*0.5;
TAU_max = 15;
TAU = (1:N)*(TAU_max/N)
TAU = 1×6
2.5000 5.0000 7.5000 10.0000 12.5000 15.0000

Iniciar sesión para comentar.

Respuestas (2)

Voss
Voss el 28 de Feb. de 2022
Editada: Voss el 28 de Feb. de 2022
Pre-allocate TAU as a vector of zeros, then just set elements 2 through N-1:
N = 12*0.5;
TAU_max = 15;
TAU = zeros(N,1); % pre-allocate
for i = 2:N-1
TAU(i,:) = i*TAU_max/N;
end
disp(TAU);
0 5.0000 7.5000 10.0000 12.5000 0
Alternatively, set all elements and afterward set elements 1 and N to zero:
N = 12*0.5;
TAU_max = 15;
TAU = zeros(N,1);
for i = 1:N
TAU(i,:) = i*TAU_max/N;
end
TAU([1 N],:) = 0;
disp(TAU);
0 5.0000 7.5000 10.0000 12.5000 0

Arif Hoq
Arif Hoq el 28 de Feb. de 2022
Editada: Arif Hoq el 28 de Feb. de 2022
try this:
N = 12*0.5;
TAU_max = 15;
for i = 1:N
TAU(i,:) = i*(TAU_max/N);
TAU([1 N],:)=0;
end
disp(TAU)
0 5.0000 7.5000 10.0000 12.5000 0

Categorías

Más información sobre Loops and Conditional Statements 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