3d matrix inline initialization












0














Is there any way to inline initialize a 3D matrix in MATLAB in a single line? Thus, without use of for-loops and pre-initialization, e.g. via zero(a,b,c). As far as I know we can only do 2D as in:



matr=[1,2;3,4]


where , and ; indicate the two dimensions. Is there any deliminator for the third dimension that I do not know about? I know that the a multi-line initilization is possible via



matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5];
matr(:,:,2) = [0 3 2; 8 8 4; 5 3 5];
matr(:,:,3) = [6 4 7; 6 8 5; 5 4 3];









share|improve this question
























  • Here is the official doc concerning the creation/manipulation of multi-dimensional arrays uk.mathworks.com/help/matlab/math/multidimensional-arrays.html
    – marsei
    Jan 11 '16 at 14:17










  • This is weird bahaviour?!? matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5]; initialises a 3x3 double, and not a 3x3x1 double; then the second line suddenly is able to add a dimension to an existing array. I always thought that'd give you a "dimension mismatch" error. Seems like you learn something every day
    – Adriaan
    Jan 11 '16 at 14:41


















0














Is there any way to inline initialize a 3D matrix in MATLAB in a single line? Thus, without use of for-loops and pre-initialization, e.g. via zero(a,b,c). As far as I know we can only do 2D as in:



matr=[1,2;3,4]


where , and ; indicate the two dimensions. Is there any deliminator for the third dimension that I do not know about? I know that the a multi-line initilization is possible via



matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5];
matr(:,:,2) = [0 3 2; 8 8 4; 5 3 5];
matr(:,:,3) = [6 4 7; 6 8 5; 5 4 3];









share|improve this question
























  • Here is the official doc concerning the creation/manipulation of multi-dimensional arrays uk.mathworks.com/help/matlab/math/multidimensional-arrays.html
    – marsei
    Jan 11 '16 at 14:17










  • This is weird bahaviour?!? matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5]; initialises a 3x3 double, and not a 3x3x1 double; then the second line suddenly is able to add a dimension to an existing array. I always thought that'd give you a "dimension mismatch" error. Seems like you learn something every day
    – Adriaan
    Jan 11 '16 at 14:41
















0












0








0







Is there any way to inline initialize a 3D matrix in MATLAB in a single line? Thus, without use of for-loops and pre-initialization, e.g. via zero(a,b,c). As far as I know we can only do 2D as in:



matr=[1,2;3,4]


where , and ; indicate the two dimensions. Is there any deliminator for the third dimension that I do not know about? I know that the a multi-line initilization is possible via



matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5];
matr(:,:,2) = [0 3 2; 8 8 4; 5 3 5];
matr(:,:,3) = [6 4 7; 6 8 5; 5 4 3];









share|improve this question















Is there any way to inline initialize a 3D matrix in MATLAB in a single line? Thus, without use of for-loops and pre-initialization, e.g. via zero(a,b,c). As far as I know we can only do 2D as in:



matr=[1,2;3,4]


where , and ; indicate the two dimensions. Is there any deliminator for the third dimension that I do not know about? I know that the a multi-line initilization is possible via



matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5];
matr(:,:,2) = [0 3 2; 8 8 4; 5 3 5];
matr(:,:,3) = [6 4 7; 6 8 5; 5 4 3];






matlab multidimensional-array initialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 3:06









Cœur

17.4k9102143




17.4k9102143










asked Jan 11 '16 at 9:47









Sebastian

8981119




8981119












  • Here is the official doc concerning the creation/manipulation of multi-dimensional arrays uk.mathworks.com/help/matlab/math/multidimensional-arrays.html
    – marsei
    Jan 11 '16 at 14:17










  • This is weird bahaviour?!? matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5]; initialises a 3x3 double, and not a 3x3x1 double; then the second line suddenly is able to add a dimension to an existing array. I always thought that'd give you a "dimension mismatch" error. Seems like you learn something every day
    – Adriaan
    Jan 11 '16 at 14:41




















  • Here is the official doc concerning the creation/manipulation of multi-dimensional arrays uk.mathworks.com/help/matlab/math/multidimensional-arrays.html
    – marsei
    Jan 11 '16 at 14:17










  • This is weird bahaviour?!? matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5]; initialises a 3x3 double, and not a 3x3x1 double; then the second line suddenly is able to add a dimension to an existing array. I always thought that'd give you a "dimension mismatch" error. Seems like you learn something every day
    – Adriaan
    Jan 11 '16 at 14:41


















Here is the official doc concerning the creation/manipulation of multi-dimensional arrays uk.mathworks.com/help/matlab/math/multidimensional-arrays.html
– marsei
Jan 11 '16 at 14:17




Here is the official doc concerning the creation/manipulation of multi-dimensional arrays uk.mathworks.com/help/matlab/math/multidimensional-arrays.html
– marsei
Jan 11 '16 at 14:17












This is weird bahaviour?!? matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5]; initialises a 3x3 double, and not a 3x3x1 double; then the second line suddenly is able to add a dimension to an existing array. I always thought that'd give you a "dimension mismatch" error. Seems like you learn something every day
– Adriaan
Jan 11 '16 at 14:41






This is weird bahaviour?!? matr(:,:,1) = [1 2 3; 9 8 7; 4 6 5]; initialises a 3x3 double, and not a 3x3x1 double; then the second line suddenly is able to add a dimension to an existing array. I always thought that'd give you a "dimension mismatch" error. Seems like you learn something every day
– Adriaan
Jan 11 '16 at 14:41














1 Answer
1






active

oldest

votes


















2














Use cat to concatenate along the third dimension:



cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3])


You can also achieve this using reshape:



reshape([[1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3]], [3,3,3])





share|improve this answer























  • not pretty but works, thanks!
    – Sebastian
    Jan 11 '16 at 10:06








  • 1




    The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
    – Nemesis
    Jan 11 '16 at 12:02










  • @Nemesis see edit
    – Dan
    Jan 11 '16 at 12:06










  • Looks good (and better than my transpose and permute).
    – Nemesis
    Jan 11 '16 at 12:40










  • @Nemesis still a poor choice compared to cat though
    – Dan
    Jan 11 '16 at 12:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34718357%2f3d-matrix-inline-initialization%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Use cat to concatenate along the third dimension:



cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3])


You can also achieve this using reshape:



reshape([[1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3]], [3,3,3])





share|improve this answer























  • not pretty but works, thanks!
    – Sebastian
    Jan 11 '16 at 10:06








  • 1




    The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
    – Nemesis
    Jan 11 '16 at 12:02










  • @Nemesis see edit
    – Dan
    Jan 11 '16 at 12:06










  • Looks good (and better than my transpose and permute).
    – Nemesis
    Jan 11 '16 at 12:40










  • @Nemesis still a poor choice compared to cat though
    – Dan
    Jan 11 '16 at 12:44
















2














Use cat to concatenate along the third dimension:



cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3])


You can also achieve this using reshape:



reshape([[1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3]], [3,3,3])





share|improve this answer























  • not pretty but works, thanks!
    – Sebastian
    Jan 11 '16 at 10:06








  • 1




    The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
    – Nemesis
    Jan 11 '16 at 12:02










  • @Nemesis see edit
    – Dan
    Jan 11 '16 at 12:06










  • Looks good (and better than my transpose and permute).
    – Nemesis
    Jan 11 '16 at 12:40










  • @Nemesis still a poor choice compared to cat though
    – Dan
    Jan 11 '16 at 12:44














2












2








2






Use cat to concatenate along the third dimension:



cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3])


You can also achieve this using reshape:



reshape([[1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3]], [3,3,3])





share|improve this answer














Use cat to concatenate along the third dimension:



cat(3, [1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3])


You can also achieve this using reshape:



reshape([[1 2 3; 9 8 7; 4 6 5], [0 3 2; 8 8 4; 5 3 5], [6 4 7; 6 8 5; 5 4 3]], [3,3,3])






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 11 '16 at 12:06

























answered Jan 11 '16 at 10:03









Dan

36.8k95299




36.8k95299












  • not pretty but works, thanks!
    – Sebastian
    Jan 11 '16 at 10:06








  • 1




    The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
    – Nemesis
    Jan 11 '16 at 12:02










  • @Nemesis see edit
    – Dan
    Jan 11 '16 at 12:06










  • Looks good (and better than my transpose and permute).
    – Nemesis
    Jan 11 '16 at 12:40










  • @Nemesis still a poor choice compared to cat though
    – Dan
    Jan 11 '16 at 12:44


















  • not pretty but works, thanks!
    – Sebastian
    Jan 11 '16 at 10:06








  • 1




    The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
    – Nemesis
    Jan 11 '16 at 12:02










  • @Nemesis see edit
    – Dan
    Jan 11 '16 at 12:06










  • Looks good (and better than my transpose and permute).
    – Nemesis
    Jan 11 '16 at 12:40










  • @Nemesis still a poor choice compared to cat though
    – Dan
    Jan 11 '16 at 12:44
















not pretty but works, thanks!
– Sebastian
Jan 11 '16 at 10:06






not pretty but works, thanks!
– Sebastian
Jan 11 '16 at 10:06






1




1




The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
– Nemesis
Jan 11 '16 at 12:02




The second one does not give the correct results (actually, this was what I was trying first, too). Here, permute(reshape([1 2 3; 9 8 7; 4 6 5;0 3 2; 8 8 4; 5 3 5; 6 4 7; 6 8 5; 5 4 3].',[3,3,3]),[2,1,3]) gives the correct matrix.
– Nemesis
Jan 11 '16 at 12:02












@Nemesis see edit
– Dan
Jan 11 '16 at 12:06




@Nemesis see edit
– Dan
Jan 11 '16 at 12:06












Looks good (and better than my transpose and permute).
– Nemesis
Jan 11 '16 at 12:40




Looks good (and better than my transpose and permute).
– Nemesis
Jan 11 '16 at 12:40












@Nemesis still a poor choice compared to cat though
– Dan
Jan 11 '16 at 12:44




@Nemesis still a poor choice compared to cat though
– Dan
Jan 11 '16 at 12:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f34718357%2f3d-matrix-inline-initialization%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'