Karolina
Karolina

Reputation: 109

how to convert two-columned cell array into matrix with points (each pair of elements from each row of cell array) MATLAB

I have an cell Array (input) like this:

A(1,1) = {[1 2]};   A(1,2) = {[4 5 6]};

now for each row of A (in this case only 1) I would like to obtian the vector of points like this:

A_row1 =[ 1 4; 1 5; 1 6; 2 4; 2 5; 2 6]

I wonder if there is any method to cope with this without a loop?

Upvotes: 2

Views: 120

Answers (2)

Huguenot
Huguenot

Reputation: 2447

How about:

[x, y] = ndgrid(A{1}, A{2})
B = [x(:) y(:)]

Upvotes: 3

Stewie Griffin
Stewie Griffin

Reputation: 14939

I think this should do the trick:

B = sortrows([repmat(A{1}',size(A{2},2),1) repmat(A{2}',size(A{1},2),1)])

B =

 1     4
 1     5
 1     6
 2     4
 2     5
 2     6

Upvotes: 1

Related Questions