Assigning Null / Multi-Dimensional Matrix

4 vues (au cours des 30 derniers jours)
tinkyminky93
tinkyminky93 le 7 Avr 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 commentaires
KSSV
KSSV le 7 Avr 2022
Question is not clear. What you want to do in the loop?
tinkyminky93
tinkyminky93 le 7 Avr 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

Connectez-vous pour commenter.

Réponse acceptée

Arif Hoq
Arif Hoq le 7 Avr 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

Plus de réponses (1)

KSSV
KSSV le 7 Avr 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 commentaire
tinkyminky93
tinkyminky93 le 7 Avr 2022
Modifié(e) : tinkyminky93 le 7 Avr 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by