to convert a row into 3d matrix having all combinations
Show older comments
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);
Ahmed Mahfouz
on 7 Feb 2018
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
ABDUL
on 7 Feb 2018
Guillaume
on 7 Feb 2018
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);
Ahmed Mahfouz
on 7 Feb 2018
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
ABDUL
on 8 Feb 2018
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.
Guillaume
on 8 Feb 2018
i want the result ...
Then why did you accept an answer that did not do at all what you wanted?
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);
ABDUL
on 8 Feb 2018
Matt J
on 8 Feb 2018
B=cat(1,result,result);
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!