I'm very new to Matlab and want to create a matrix that is 2xn where the first column is all 1's and the second column increments 1-100. I know how to write a for loop to get an incrementing vector and I know how to create a matrix of all ones, but I can't figure out a for loop that will retrain the 1 in the first column. EG) I want my matrix A to look like [1,1;1,2;1,3;...;1,n].

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 2월 11일
편집: Azzi Abdelmalek 2014년 2월 11일

0 개 추천

A=ones(10,2)
A(:,2)=1:10
%or
A=[ones(10,1) (1:10)']
%If you want a for loop
A=zeros(10,2)
for k=1:10
A(k,1:2)=[1 k]
end

추가 답변 (1개)

Darin McCoy
Darin McCoy 2014년 2월 11일

0 개 추천

n = 100;
[ones(n,1) (1:n)'];

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

Kat
2014년 2월 11일

편집:

2014년 2월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by