MATLAB Answers

to convert a row into 3d matrix having all combinations

1 view (last 30 days)
ABDUL
ABDUL on 7 Feb 2018
Commented: Matt J on 8 Feb 2018
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 .

Answers (2)

Matt J
Matt J on 7 Feb 2018
Edited: Matt J on 7 Feb 2018
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);

  0 Comments

Sign in to comment.


Ahmed Taha
Ahmed Taha 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

Show 7 older comments
Matt J
Matt J on 8 Feb 2018
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
ABDUL on 8 Feb 2018
if i want to retain the same size of 512x4 what do i have to modify.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!