Effacer les filtres
Effacer les filtres

How do I add two columns to a matrix in ascending order?

1 vue (au cours des 30 derniers jours)
Glenn Roumen
Glenn Roumen le 11 Juin 2013
I need a matrix which numbers ascend in the following way:
If I have the columns:
x=[1 2 3]
y=[4 5 6]
I want the matrix to be like:
A=[1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6]
I hope someone can help me with this.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 11 Juin 2013
x=[1 2 3]
y=[4 5 6]
x1=repmat(x,numel(y),1)
out=[x1(:) repmat(y',numel(x),1)]
  5 commentaires
Glenn Roumen
Glenn Roumen le 11 Juin 2013
I got it myself. Thank you for the answer. I used this code:
x1=repmat(i,numel(j),1);
x1=sortrows(x1);
out=[x1(:) repmat(j,numel(i),1)]
Glenn Roumen
Glenn Roumen le 11 Juin 2013
I meant to say 1 in stead of 001. But I allready got the solution I wanted. Thanks

Connectez-vous pour commenter.

Plus de réponses (2)

Iain
Iain le 11 Juin 2013
x = repmat(x,numel(y),1)
y = repmat(y,1,size(x,2))
A = [x(:) y(:)]

Andrei Bobrov
Andrei Bobrov le 11 Juin 2013
[ii,jj] = ndgrid(y,x);
out = [jj(:), ii(:)];

Catégories

En savoir plus sur Logical 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