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

20 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-4-27
编辑:Stephen23 2020-4-27
"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.

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2020-4-27
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 个评论
Walter Roberson
Walter Roberson 2020-4-27
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by