How do you convert an array into a square matrix?

Say I have an array
x=[1 2 3 4 5 6 7 8 9 10]
and I want to convert this into a square matrix as follows:
M=
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10...
And so on so that there are a total of 10 rows and 10 columns
So I start with a [1x10]array and turn this into a [10x10] matrix
How do you do this??
Thank you!
MC

댓글 수: 1

It's often possible to avoid replicating data (and also desirable for memory-efficiency reasons) using the BSXFUN command. You should elaborate on why you think you need the rows replicated.

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

 채택된 답변

Wayne King
Wayne King 2012년 9월 29일
편집: Wayne King 2012년 9월 29일
x = [1 2 3 4 5 6 7 8 9 10];
x = repmat(x,10,1);

추가 답변 (2개)

Matt Fig
Matt Fig 2012년 9월 29일
편집: Matt Fig 2012년 9월 29일
Another:
x = 1:10; % Original
x = x(ones(1,length(x)),:) % indexing instead of repmatting.
Andrei Bobrov
Andrei Bobrov 2012년 9월 30일
M = ones(numel(x),1)*x;
M = bsxfun(@plus,x,zeros(numel(x),1));

카테고리

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

질문:

2012년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by