From a vector to a 3D array

1 view (last 30 days)
trey
trey on 16 Aug 2018
Commented: trey on 29 Aug 2018
I would like to create a 3D array from a vector. As an example, the size of the could be 6x5x4 and the vector is 120x1.
I tried to reshape function but the elements order of the array "result" are not correct
I copied the solution that I should obtain for the array result for a (:,:,1)
% result(:,:,1) =
Columns 1 through 4
0.353158571222071 0.73172238565867 0.188955015032545 0.0811257688657853
0.821194040197959 0.647745963136307 0.686775433365315 0.92938597096873
0.0154034376515551 0.450923706430945 0.18351115573727 0.775712678608402
0.0430238016578078 0.547008892286345 0.368484596490336 0.486791632403172
0.168990029462704 0.296320805607773 0.62561856072969 0.435858588580919
0.649115474956452 0.744692807074156 0.780227435151377 0.446783749429806
Column 5
0.306349472016557
0.508508655381127
0.51077156417211
0.817627708322262
0.794831416883453
0.644318130193692
I attached the vector file.
  1 Comment
Stephen23
Stephen23 on 16 Aug 2018
Edited: Stephen23 on 16 Aug 2018
"I tried to reshape function but the elements order of the array "result" are not correct"
Sure. But you did not tell us what the "correct" order should be.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Aug 2018
Edited: Stephen23 on 17 Aug 2018
A simple reshape gives the exact order that you request:
>> mat = reshape(vector,[6,5,4])
mat(:,:,1) =
0.353158571222071 0.73172238565867 0.188955015032545 0.08112576886578526 0.306349472016557
0.8211940401979591 0.6477459631363069 0.686775433365315 0.92938597096873 0.508508655381127
0.01540343765155505 0.450923706430945 0.18351115573727 0.775712678608402 0.51077156417211
0.04302380165780784 0.547008892286345 0.368484596490336 0.486791632403172 0.8176277083222619
0.168990029462704 0.296320805607773 0.62561856072969 0.435858588580919 0.794831416883453
0.6491154749564521 0.744692807074156 0.780227435151377 0.446783749429806 0.644318130193692
mat(:,:,2) =
0.378609382660268 0.550156342898422 0.230488160211558 0.435698684103899 0.979748378356085
0.811580458282477 0.622475086001227 0.8443087926953891 0.311102286650413 0.438869973126103
0.532825588799455 0.587044704531417 0.194764289567049 0.923379642103244 0.111119223440599
0.350727103576883 0.207742292733028 0.225921780972399 0.430207391329584 0.258064695912067
0.939001561999887 0.301246330279491 0.170708047147859 0.184816320124136 0.408719846112552
0.875942811492984 0.470923348517591 0.227664297816554 0.904880968679893 0.594896074008614
mat(:,:,3) =
0.262211747780845 0.318778301925882 0.02922027756214629 0.458848828179931 0.62406008817369
0.602843089382083 0.424166759713807 0.928854139478045 0.963088539286913 0.679135540865748
0.7112157804336831 0.507858284661118 0.730330862855453 0.546805718738968 0.395515215668593
0.22174673401724 0.08551579709004398 0.488608973803579 0.5211358308040011 0.367436648544477
0.117417650855806 0.262482234698333 0.578525061023439 0.231594386708524 0.987982003161633
0.296675873218327 0.801014622769739 0.237283579771521 0.488897743920167 0.03773886623955214
mat(:,:,4) =
0.885168008202475 0.679727951377338 0.779051723231275 0.197809826685929 0.609866648422558
0.913286827639239 0.13655313735537 0.715037078400694 0.03054094630463666 0.617666389588455
0.796183873585212 0.72122749858174 0.903720560556316 0.744074260367462 0.859442305646212
0.0987122786555743 0.106761861607241 0.890922504330789 0.500022435590201 0.805489424529686
0.261871183870716 0.6537573486685599 0.334163052737496 0.47992214114606 0.576721515614685
0.335356839962797 0.49417393663927 0.698745832334794 0.904722238067363 0.182922469414914
And checking:
"See that for (2,1,1) the result that I need is 0.821194040197959"
>> mat(2,1,1)
ans = 0.8211940401979591

More Answers (1)

trey
trey on 17 Aug 2018
@Stephen Cobeldick, thank you for your quick replay.
I copied the results that I have to obtain. The order that you propose is not the correct that I need.
See that for (2,1,1) the result that I need is 0.821194040197959 and in the code you proposed is 0.649115
% result(:,:,1) =
Columns 1 through 4
0.353158571222071 0.73172238565867 0.188955015032545 0.0811257688657853
0.821194040197959 0.647745963136307 0.686775433365315 0.92938597096873
0.0154034376515551 0.450923706430945 0.18351115573727 0.775712678608402
0.0430238016578078 0.547008892286345 0.368484596490336 0.486791632403172
0.168990029462704 0.296320805607773 0.62561856072969 0.435858588580919
0.649115474956452 0.744692807074156 0.780227435151377 0.446783749429806
Column 5
0.306349472016557
0.508508655381127
0.51077156417211
0.817627708322262
0.794831416883453
0.644318130193692
  2 Comments
Stephen23
Stephen23 on 17 Aug 2018
@trey: see my edited answer.
trey
trey on 29 Aug 2018
@Stephen sorry I was on holiday ! your solution was perfect for my problem. thank you :)

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices 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!