필터 지우기
필터 지우기

how to replace a column in a matrix

조회 수: 46 (최근 30일)
ZHEN XU
ZHEN XU 2019년 5월 15일
댓글: ZHEN XU 2019년 5월 15일
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
James Tursa
James Tursa 2019년 5월 15일
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 2019년 5월 15일
편집: ZHEN XU 2019년 5월 15일
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

댓글을 달려면 로그인하십시오.

채택된 답변

James Tursa
James Tursa 2019년 5월 15일
편집: James Tursa 2019년 5월 15일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by