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

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.

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 11 日

1 投票

x=[1 2 3]
y=[4 5 6]
x1=repmat(x,numel(y),1)
out=[x1(:) repmat(y',numel(x),1)]

5 件のコメント

Glenn Roumen
Glenn Roumen 2013 年 6 月 11 日
Thank you for your answer but it still does not work for the case I have. I have a column of 514 elements and a column of 80 elements. When I compute the code it gives something like:
A=[. .
. .
. .
512 38
513 39
514 40]
But I actually need:
A=[001 01
001 02
001 03
. .
. .
. .
514 38
514 39
514 40]
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 6 月 11 日
What are the sizes of x and y, type
size(y)
Jan
Jan 2013 年 6 月 11 日
@Glenn: What does "001" exactly mean? Numbers do not have leading zeros. If you are talking of strings, please explain this explicitly.
I think Azzi's solution does create the wanted result. Did you copy his code exactly?
Glenn Roumen
Glenn Roumen 2013 年 6 月 11 日
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 2013 年 6 月 11 日
I meant to say 1 in stead of 001. But I allready got the solution I wanted. Thanks

サインインしてコメントする。

その他の回答 (2 件)

Iain
Iain 2013 年 6 月 11 日

0 投票

x = repmat(x,numel(y),1)
y = repmat(y,1,size(x,2))
A = [x(:) y(:)]
Andrei Bobrov
Andrei Bobrov 2013 年 6 月 11 日

0 投票

[ii,jj] = ndgrid(y,x);
out = [jj(:), ii(:)];

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by