Effacer les filtres
Effacer les filtres

how to replace a column in a matrix

44 vues (au cours des 30 derniers jours)
ZHEN XU
ZHEN XU le 15 Mai 2019
Commenté : ZHEN XU le 15 Mai 2019
I wanna know how to replace a column in below condition:
Assmuing I had a 5*5 matrix, I wanna replace each column by zeros(5,1) for 5 times. I don't mean to get a 5*5 all 0 element matrix eventually. I want to get 5 matrix with their 1st,2nd,3rd,4th,5th column be replaced individually. How could I code for this? Thanks.
  2 commentaires
James Tursa
James Tursa le 15 Mai 2019
What have you done so far? What specific problems are you having with your code? Can you write a loop and put the results into a cell array?
ZHEN XU
ZHEN XU le 15 Mai 2019
Modifié(e) : ZHEN XU le 15 Mai 2019
A = rand(5,5);
B = zeros(5,1);
m = 5;
for i = 1:m
A(:,i) = B;
end
This is what I wrote, but It gave me a matrix with all column got replaced... I wanna 5 matrix outcome, only one colume got replaced at a time

Connectez-vous pour commenter.

Réponse acceptée

James Tursa
James Tursa le 15 Mai 2019
Modifié(e) : James Tursa le 15 Mai 2019
Instead of replacing all of the columns of A, use a different variable for the result. E.g., a cell array named C. So instead of this:
A(:,i) = B;
you could have this
C{i} = A;
C{i}(:,i) = B;
Then each of C{1}, C{2}, etc contain your matrices
  1 commentaire
ZHEN XU
ZHEN XU le 15 Mai 2019
Thank you. It works.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by