Problems with indexing in multidimensional array
Show older comments
Hi Community!
I have trouble understanding how matlab handles dimensions if I loop though one of the inner dimensions and try to build a result array which has the same size as the original one.
I have written an example:
clear test;
clear test2;
test = ones(4,1,1,3,3,4);
size(test)
for ii = 1:size(test,4)
test2(:,:,:,ii,:,:) = ii*test(:,:,:,ii,:,:);
end
size(test2)
The size of test changes from 4,1,1,3,3,4 to test2 size of 4,3,4,3. My intention was to take the original array and multiply every element where the 4.dimension is 1 by 1, where it is 2 by 2 and so on. My expected result array test2 would have the exact same dimensions as test.
The multiplication by ii is just an example. In my original code I do a more complex calculation which I can't substitute by a matrix operation.
What am I doing wrong? What's the correct way to achieve my goal?
Thanks in advance folks!
1 Comment
"What's the correct way to achieve my goal?"
Preallocate the array:
Accepted Answer
More Answers (0)
Categories
Find more on Creating and Concatenating 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!