Clear Filters
Clear Filters

How do I get rid of a dimension of length 0 in a multi-dimensional array?

20 views (last 30 days)
Some how I've created an array with a dimension of length 0. Squeeze does not git rid of it. Reshape does not work either.
K>> whos data
Name Size Bytes Class Attributes
data 410x410x0 0 double
K>> aux = squeeze(data);
K>> whos aux
Name Size Bytes Class Attributes
aux 410x410x0 0 double
K>> aux = reshape(data,[size(data,1),size(data,2)]);
Error using reshape
To RESHAPE the number of elements must not change.
>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.3.0.713579 (R2017b)
MATLAB License Number: 339958
Operating System: Linux 3.10.0-1062.18.1.el7.x86_64 #1 SMP Tue Mar 17 23:49:17 UTC 2020 x86_64
Java Version: Java 1.8.0_121-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
  1 Comment
Stephen23
Stephen23 on 27 Apr 2020
Edited: Stephen23 on 27 Apr 2020
"Squeeze does not git rid of it."
The squeeze documentation states that it will "Remove dimensions of length 1". Is zero equal to one? Why do you expect zero-length dimensions to be removed by a function which states that it removes one-length dimensions?
"Reshape does not work either"
The reshape function changes the size of the input array into another array with the same number of elements as the input array. Your array has zero elements:
>> A = nan(410,410,0);
>> numel(A)
ans = 0
>> 410*410*0
ans = 0
and so you can reshape it into any array with the same number of elements, all of these will work without error:
>> reshape(A,5,0,23)
>> reshape(A,0,123456789)
>> reshape(A,0,0,0,0,0)
etc.
There are countless empty arrays that you could obtain from your empty array.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 27 Apr 2020
A variable that has a 0 dimension is an empty variable ... there is no data in it to reshape. You need to backtrack in your code and figure out why your algorithm is producing an empty variable and fix that. No amount of squeezing or reshaping is going to get back data that isn't there.
  1 Comment
Walter Roberson
Walter Roberson on 27 Apr 2020
Suppose squeeze() of the 410 x 410 x 0 resulted in a 410 x 410 array, as it seems you would like to happen. 410 x 410 x 0 has no data associated with it, but 410 x 410 has 168100 elements associated with it. Where should those 168100 elements come from?
Imagine that you are building a multi-story worm farm. If you had 410 rows and 410 columns and 1 level, you would have space for (410*410*1) = 168100 worms. If you had 2 levels, you would have space for (410*410*2) = 336200 worms; if you had 3 levels, you would have space for (410*410*3) = 504300 worms. Now let's go the other way, down to no levels built yet: that would have room for (410*410*0) = 0 worms.
What is the meaning associated with 410 x 410 x 0 ? Well, for example, it might mean that you had created the platform for the worm farm and marked in it where all the edges of the cells are going to go, but had not yet built up one layer, just stakes and rope in the ground without even a single roof yet. No matter how you rearrange the stakes, just rearranging them does not create any roofs.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!