필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Glenn Roumen
Glenn Roumen 2013년 6월 11일
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일
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일
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일
x = repmat(x,numel(y),1)
y = repmat(y,1,size(x,2))
A = [x(:) y(:)]

Andrei Bobrov
Andrei Bobrov 2013년 6월 11일
[ii,jj] = ndgrid(y,x);
out = [jj(:), ii(:)];

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by