How do you change an entire row or column of an array to a certain value

163 visualizaciones (últimos 30 días)
If I have an array of all zeros X = zeros(N) How do I change a column to a value I want.

Respuesta aceptada

Star Strider
Star Strider el 15 de Sept. de 2017
One option:
N = 5;
X = zeros(N);
X(:,3) = rand(N, 1); % Change Column #3 To A Specific Vector
X(2,:) = 5*ones(1, N); % Change Row #2 To a Row Of ‘5’
  4 comentarios
Riley Harris
Riley Harris el 13 de Sept. de 2023
Can this be done using a for loop and if/elseif statements?
Star Strider
Star Strider el 13 de Sept. de 2023
@Riley Harris — Yes, however it can easily be vectorised. Using the sub2ind function is another option.
See the documentation section on Matrix Indexing for details.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing 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