How can I fill column elements with the column number. First column contains only 1, second only 2, third only 3, etc.

댓글 수: 1

Is it possible to do this without cycle? I mean without 'for'?

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

 채택된 답변

Hayden Garmon
Hayden Garmon 2020년 9월 14일
편집: Hayden Garmon 2020년 9월 14일

1 개 추천

m=5;% m is length of columns
n=4;% n is number of columns
for i =1:n
A(:,i)=ones(m,1)*i ;
end
A =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

댓글 수: 4

Is it possible to do this without cycle? I mean without 'for'?
m=5;
n=4;
i =1:n; % n is number of columns
A(:,i)=ones(m,1)*i ;% m is length of columns
% Yes it is
Thanks!
madhan ravi
madhan ravi 2020년 9월 14일
편집: madhan ravi 2020년 9월 14일
+1
A = ones(m, 1) * ii
%or
A = repmat(ii, m, 1)

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2020년 9월 14일

편집:

2020년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by