Problems with indexing in multidimensional array

1 回表示 (過去 30 日間)
Michael
Michael 2017 年 3 月 20 日
コメント済み: Michael 2017 年 3 月 20 日
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 件のコメント
Stephen23
Stephen23 2017 年 3 月 20 日
編集済み: Stephen23 2017 年 3 月 20 日
"What's the correct way to achieve my goal?"
Preallocate the array:

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 3 月 20 日
編集済み: Guillaume 2017 年 3 月 20 日
Well, to see the problem, simply do:
clear test;
clear test2;
test = ones(4,1,1,3,3,4);
test2(:, :, :, 1, :, :) = test(:, :, :, 1, :, :);
size(test2)
The reason is that you're assigning something 4x1x1x1x3x4 to something that does not exists (test2). So matlab creates a matrix big enough to hold whatever your assigning, ignoring the singular dimensions. So you get a 4x3x4 matrix.
To solve this you need to tell matlab what the size of test2 is supposed to be, before you start the loop, so:
test2 = zeros(size(test));
for ...
and matlab won't have to guess (and get it wrong) what shape test2 is supposed to be. Other benefits of this:
  • faster code. With your code, the size of test2 increases at each step of the loop, so matlab has to reallocate a new array at each step, copy the old one over and add the new elements
  • no need for clear anymore, since if the variables already exist, you're replacing them.
Note that if you'd written your code in the editor, you'd have seen a wiggly line under test2 in the loop telling you that test2 changes size at each iteration and to consider preallocation.
  2 件のコメント
Stephen23
Stephen23 2017 年 3 月 20 日
+1 for a clear explanation, and for pointing out that that editor gives useful advice.
Michael
Michael 2017 年 3 月 20 日
Thanks a lot. I already knew that my method is bad in terms of performance. But I did not see the problem with the singleton dimension. As you wrote it creates the 4x3x4 matrix in the first loop run and in the second run the test2(:,:,:,ii,:,:) command adds another dimension so that I end up with 4x3x4x3.
Thanks for the quick reply!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by