简体   繁体   English

使用end来增长数组时出现意外行为

[英]Unexpected behavior while using end to grow arrays

while building a video array from a directory of images I encounter unexpected behavior. 从图像目录构建视频数组时遇到意外行为。 Original code: 原始代码:

vid = [];
for i =startframe:endframe
    image = [directoryOfImages ,'\', images_names{1,i}];
    vid(:,:,:,end+1) = imread(image);
    waitbar((i-startframe) / (endframe-startframe));
end

Then I ran this code to check thing up: 然后我运行此代码来检查事物:

a = []; size(a)
a(end+1) = 1; size(a)

The first size was [0, 0] and the second size was [1, 1] . 第一个大小是[0, 0] ,第二个大小是[1, 1] The same expected behavior I got in this code: 我在这段代码中得到的预期行为相同:

b = []; size(b)
b(:,end+1) = 1; size(b)

The first size was [0, 0] and the second size was [1, 1] . 第一个大小是[0, 0] ,第二个大小是[1, 1] But in this code, something weird happened: 但在这段代码中,发生了一些奇怪的事情:

c = []; size(c)
c(:,:,end+1) = 1; size(c)

while here the first size was [0,0] and the second one was [1,1,2] . 而这里第一个尺寸是[0,0] ,第二个尺寸是[1,1,2] This was very unexpected. 这是非常意外的。 I printed c and I got this: 我打印了c ,我得到了这个:

>>c
c(:,:,1) =

     0

c(:,:,2) =

     1

Finally, I ran this script: 最后,我运行了这个脚本:

c=[]; c(:,:,end)=1; size(c)

and I got [1, 1] . 我得到了[1, 1]

can someone explain what is going on here? 谁能解释一下这里发生了什么? when I use c=[] do I get an empty array with the size of [0,0,1] ? 当我使用c=[] ,我得到一个大小为[0,0,1]的空数组? so how come size(c) doesn't mention it? 那么size(c)怎么没提呢? and why when I use c(:,:,end)=1; 以及为什么当我使用c(:,:,end)=1; its size is not [1,1,1] ? 它的大小不是[1,1,1] and what about when I use c(:,:,:,end)=1 ? 当我使用c(:,:,:,end)=1怎么办?

This is just MATLAB choosing what to display. 这只是MATLAB选择要显示的内容。

In MATLAB, matrices are infinite dimensional. 在MATLAB中,矩阵是无限维的。 As a nice example, lets try your b : 作为一个很好的例子,让我们试试你的b

b = []; 
b(:,end+1) = 1; 

As you know, you can query the size of an specific dimension with size . 如您所知,您可以使用大小查询特定维度的size Eg size(b,2) returns 1 . 例如, size(b,2)返回1 But what does size(b,12345) return?, well, it returns 1 also, as matrices are infinite dimensional. 但是, size(b,12345)返回什么?,它也返回1 ,因为矩阵是无限维的。 In the 12345th dimension, the size of b is 1 . 在12345维度中, b的大小为1

However, what horrible would the display function be, if every time you type size(b) it outputs an infinite amount of dimensions! 然而,显示功能会是多么可怕,如果每次输入size(b)它会输出无限量的尺寸! Thus when displaying, MATLAB defaults to displaying 2 dims OR N-dims, where N is the furthest dimension with data on it (non-singleton dimension). 因此,在显示时,MATLAB默认显示2个dims或N-dims,其中N是具有数据的最远维度(非单一维度)。

Thus, what you are seeing with your c example is weird behaviour by the display function, not the size function. 因此,您在c示例中看到的是显示函数的奇怪行为,而不是size函数。 size(c,3) returns 1 . size(c,3)返回1 This is caused also by the [] only setting the size of the first two dimensions to zero, to avoid having a MxPx0 variable when filling it up ( c(:,:,end)=img , what happens with end ?), which is essentially an empty variable. 这也是由[]仅将前两个维度的大小设置为零引起的,以避免填充时出现MxPx0变量( c(:,:,end)=imgend会发生什么?),本质上是一个空变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM