필터 지우기
필터 지우기

creating a matrix from two sets of data

조회 수: 9 (최근 30일)
Far mah
Far mah 2019년 11월 20일
댓글: Far mah 2019년 11월 20일
Hello
Lets say, I have a row of numbers :x= [1 2 3 4]. I also have a column of numbers: y= [1;2]
I need to creat another matix from x, y comibation. so x is going to be paired with each y.
c =
1 1
2 1
3 1
4 1
1 2
2 2
3 2
4 2
Thank you !

채택된 답변

Rik
Rik 2019년 11월 20일
편집: Rik 2019년 11월 20일
You can use ndgrid to generate every combination.
[X, Y] =ndgrid(x, y);
out=[X(:) Y(:)];
  댓글 수: 2
Erivelton Gualter
Erivelton Gualter 2019년 11월 20일
Nicer and simpler version.
Far mah
Far mah 2019년 11월 20일
Thanks !

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

추가 답변 (2개)

Erivelton Gualter
Erivelton Gualter 2019년 11월 20일
Here is a possible solution:
x = [1 2 3 4];
y = [1;2];
nx = length(x);
c(1:nx,1) = x;
c(1:nx,2) = y(1);
c(nx+1:2*nx,1) = x';
c(nx+1:2*nx,2) = y(2);
For future questions, read this :
Show an attempt code as well.

Yasasvi Harish Kumar
Yasasvi Harish Kumar 2019년 11월 20일
Hi,
This should help
for y = 1:2
for x = 1:4
c(4*(y-1)+x,1) = x
c(4*(y-1)+x,2) = y
end
end

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by