MATLAB Answers

creating a matrix from two sets of data

1 view (last 30 days)
Far mah
Far mah on 20 Nov 2019
Commented: Far mah on 20 Nov 2019
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 !

  0 Comments

Sign in to comment.

Accepted Answer

Rik
Rik on 20 Nov 2019
Edited: Rik on 20 Nov 2019
You can use ndgrid to generate every combination.
[X, Y] =ndgrid(x, y);
out=[X(:) Y(:)];

More Answers (2)

Erivelton Gualter
Erivelton Gualter on 20 Nov 2019
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.

  1 Comment

Sign in to comment.


Yasasvi Harish Kumar
Yasasvi Harish Kumar on 20 Nov 2019
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

  1 Comment

Sign in to comment.

Sign in to answer this question.