필터 지우기
필터 지우기

How do I add a column to an existing matrix?

조회 수: 60 (최근 30일)
lil brain
lil brain 2022년 1월 19일
댓글: Cris LaPierre 2022년 1월 19일
Hi I have this code here:
[m,n] = size(windows);
H = zeros(n,1);
for i = 1:n
r = windows(:,i);
H(i) = dfaedit(r,1,1,1);
end
Richt now it creates a new variable H. However, every time I run this code, I would like it to add the resulting column to an existing variable H_all so that it results in a matrix of columns.
Does that make sense? Help is much appreciated!

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 1월 19일
Use [] to concatenate your result to your existing variable. You'll need to force your for loop to create a column vector instead of a row vector (change shown below).
Note that this assumes that H_all will always have the same number of rows as H.
[m,n] = size(windows);
H = zeros(n,1);
for i = 1:n
r = windows(:,i);
H(i,1) = dfaedit(r,1,1,1);
end
H_all = [H_all,H]
  댓글 수: 2
lil brain
lil brain 2022년 1월 19일
Perfect! One last question. How would I need to write this if I dont want a new variable created every time? Thanks!
Cris LaPierre
Cris LaPierre 2022년 1월 19일
Meaning you just want to add to H_all directly? Specify the column of H_all to add the new data to.
[m,n] = size(windows);
c = size(H_all,2)+1;
for i = 1:n
r = windows(:,i);
H_all(i,c) = dfaedit(r,1,1,1);
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by