Assigning Null / Multi-Dimensional Matrix

4 visualizaciones (últimos 30 días)
tinkyminky93
tinkyminky93 el 7 de Abr. de 2022
Respondida: Arif Hoq el 7 de Abr. de 2022
Hello,
I have a matrix with the dimensions of 4x2500 and I am generating this matrix in for loop. I want this matrix to be 4x1250. Without multi dimension I just write A(1:length(X)) = [] but I am stuck in multi dimension. This logic does not work in my operation.
when I say
A = [1:1:2500];
A (1:1250) = [];
it works and that is what i want to do.
but in multi dimension,
for i = 1:1:4
A(i,:) = ????????
end
Can someone help me? Thanks.
  2 comentarios
KSSV
KSSV el 7 de Abr. de 2022
Question is not clear. What you want to do in the loop?
tinkyminky93
tinkyminky93 el 7 de Abr. de 2022
I have an array and I am writing this array to every row of the matrix. For example lets say I have [1:1:5]. What I do in for loop is
A(i,:) = my array
i'th row : 1 2 3 4 5
i+1'th row: 1 2 3 4 5
i+2'th row: 1 2 3 4 5
i+3'th row: 1 2 3 4 5
I want to assign null to every row of this matrix, then it will become
i'th row : 4 5
i+1'th row: 4 5
i+2'th row: 4 5
i+3'th row: 4 5

Iniciar sesión para comentar.

Respuesta aceptada

Arif Hoq
Arif Hoq el 7 de Abr. de 2022
vectorized solution is the most efficient and simple. But, still if you need for loop, try this
x=1:5;
A = repmat(x,4,1);
for i = 3:-1:1 % if you want to delete from Column 3 then index i will be started from 3.
A(:,i) = []
end

Más respuestas (1)

KSSV
KSSV el 7 de Abr. de 2022
A = repmat(1:5,4,1)
A = 4×5
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
A(:,1:3) = []
A = 4×2
4 5 4 5 4 5 4 5
  1 comentario
tinkyminky93
tinkyminky93 el 7 de Abr. de 2022
Editada: tinkyminky93 el 7 de Abr. de 2022
Can you try it with for loop? Will it be the same? I am getting error when i try
A = repmat(1:5,4,1);
for i = 1:1:4
A(:,1:3) = [];
end
>> Matrix index is out of range for deletion

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by