How do you convert an array into a square matrix?

15 vues (au cours des 30 derniers jours)
Mike
Mike le 29 Sep 2012
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 commentaire
Matt J
Matt J le 29 Sep 2012
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.

Connectez-vous pour commenter.

Réponse acceptée

Wayne King
Wayne King le 29 Sep 2012
Modifié(e) : Wayne King le 29 Sep 2012
x = [1 2 3 4 5 6 7 8 9 10];
x = repmat(x,10,1);

Plus de réponses (2)

Matt Fig
Matt Fig le 29 Sep 2012
Modifié(e) : Matt Fig le 29 Sep 2012
Another:
x = 1:10; % Original
x = x(ones(1,length(x)),:) % indexing instead of repmatting.

Andrei Bobrov
Andrei Bobrov le 30 Sep 2012
M = ones(numel(x),1)*x;
M = bsxfun(@plus,x,zeros(numel(x),1));

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by