to convert a row into 3d matrix having all combinations

i have a piece of code p=[1 -1 1j -1j] i want to convert this 1x4 matrix into 256X4x2 matrix. how to fix this bug .

2 Comments

what should go into those 256x2 entries? What bug?
how to convert it into 256x4x2 matrix

Sign in to comment.

Answers (2)

You've left us to do a lot of guessing as to what you want, but here's my guess,
[c{1:4}]=ndgrid(p);
result=reshape(cell2mat(c),256,4);
i don't know what you exactly mean but i assumed the entries to the 3d matrix are the same as in the given vector, you can try the following code:
p=[1 -1 1j -1j];
for n=1:256
Matrix(n,:)=p;
end
Matrix(:,:,2)=Matrix(:,:,1);

10 Comments

thanks... suppose i want to have all possible combinations of the vector p in the matrix.
Well, if that's really what was wanted, then a much faster and simpler way of achieving the same would be:
Matrix = repmat(p, 256, 1, 2);
If you need to get all possible permutations you can use:
Matrix =perms(p);
The size of the resulting matrix is 24x4, it will never reach 256x4x2 as 4p4=24~=4^4
i want the result in the form by taking p=[1 -1 1j -1j] as initial vector resulting matrix should have p=[1 -1 1j -1j; 1 1 -1j -1j ; -1 1 1j -1j ... ] this is the sample resulting matrix .
i want the result in the form by taking p=[1 -1 1j -1j] as initial vector resulting matrix should have p=[1 -1 1j -1j; 1 1 -1j -1j ; -1 1 1j -1j ... ] this is the sample resulting matrix .
That was what my answer (the first one posted) gave you, so I'm not sure why you haven't considered it yet. However, there are 256 combinations that can be built as you describe, so the result will form a 256x4 matrix. It is not clear why you are expecting a result that is 256x4x2.
i want the result ...
Then why did you accept an answer that did not do at all what you wanted?
Nt=2;
for i=1:Nt
p(:,:,i)=[1 -1 1i -1i]; % phase factor possible values
end
B=[];
for i=1:Nt
for b1=1:4
for b2=1:4
for b3=1:4
for b4=1:4
B=[B; [p(b1) p(b2) p(b3) p(b4)]]; % all possible combinations
end
end
end
end
end
i want to obtain the result for 3D Matrix
When I run your code, I get a 512x4 matrix, not a 256x4x2 matrix.
>> whos B
Name Size Bytes Class Attributes
B 512x4 32768 double complex
Are you just trying to get a result in which B(:,:,1) contains all 256 combinations and B(:,:,1)=B(:,:,2)? If so, I don't know why you are duplicating the data, but just take my initial proposal and modify it as follows
[c{1:4}]=ndgrid(p);
result=reshape(cell2mat(c),256,4);
B=cat(3,result,result);
if i want to retain the same size of 512x4 what do i have to modify.

Sign in to comment.

Asked:

on 7 Feb 2018

Commented:

on 8 Feb 2018

Community Treasure Hunt

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

Start Hunting!