MATLAB中[]和[1x0]之间的区别 [英] Difference between [] and [1x0] in MATLAB

查看:97
本文介绍了MATLAB中[]和[1x0]之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个循环,使用以下条目填充工作区(2011b,Windows 7、64位)中的单元格数组:

I have a loop in MATLAB that fills a cell array in my workspace (2011b, Windows 7, 64 bit) with the following entries:

my_array = 
    [1x219 uint16]
    [         138]
    [1x0   uint16] <---- row #3
    [1x2   uint16]
    [1x0   uint16]
    []             <---- row #6
    [         210]
    [1x7   uint16]
    [1x0   uint16]
    [1x4   uint16]
    [1x0   uint16]
    [         280]
    []
    []
    [         293]
    [         295]
    [1x2   uint16]
    [         298]
    [1x0   uint16]
    [1x8   uint16]
    [1x5   uint16]

请注意,某些条目在行#6中保持[],而其他条目在行#3中保持[1x0].

Note that some entries hold [], as in row #6, while others hold [1x0] items, as in row #3.

  1. 它们之间有有什么不同吗? (除了MATLAB以不同的方式显示它们之外). MATLAB在内存中如何表示它们的任何区别?
  2. 如果区别仅在于MATLAB在内部如何表示它们,为什么程序员应意识到这一区别? (即,为什么要以不同的方式显示它们?).它是(无害的)错误吗?还是知道这些数组以不同的方式表示,有什么好处?
  1. Is there any difference between them? (other than the fact that MATLAB displays them differently). Any differences in how MATLAB represents them in memory?
  2. If the difference is only about how MATLAB internally represents them, why should the programmer be aware of this difference ? (i.e. why display them differently?). Is it a (harmless) bug? or is there any benefit in knowing that such arrays are represented differently?

推荐答案

在大多数情况下(见下文),没有真正的区别.两者均被视为空" ,因为至少一个维度的大小的0.但是,我不会将其称为bug,因为作为程序员,您可能希望在某些情况下看到此信息.

In most cases (see below for an exception) there is no real difference. Both are considered "empty", since at least one dimension has a size of 0. However, I wouldn't call this a bug, since as a programmer you may want to see this information in some cases.

例如,您有一个二维矩阵,您想索引一些行和一些列以提取到一个较小的矩阵中:

Say, for example, you have a 2-D matrix and you want to index some rows and some columns to extract into a smaller matrix:

>> M = magic(4)  %# Create a 4-by-4 matrix

M =
    16     2     3    13
     5    11    10     8
     9     7     6    12
     4    14    15     1

>> rowIndex = [1 3];  %# A set of row indices
>> columnIndex = [];  %# A set of column indices, which happen to be empty
>> subM = M(rowIndex,columnIndex)

subM =
   Empty matrix: 2-by-0

请注意,空结果仍会告诉您一些信息,特别是您尝试从原始矩阵索引2行.如果结果仅显示[],您将不会知道它是否为空,因为行索引为空,或者列索引​​为空,或者两者都为空.

Note that the empty result still tells you some information, specifically that you tried to index 2 rows from the original matrix. If the result just showed [], you wouldn't know if it was empty because your row indices were empty, or your column indices were empty, or both.

在某些情况下,定义为[]的空矩阵(即其所有维度均为0)可能会带来与仍然具有一些非零维度的空矩阵不同的结果.例如,当处理不同类型的空矩阵时,矩阵乘法可以为您提供不同的(并且有些不直观)结果.让我们考虑以下3个空矩阵:

There are some cases when an empty matrix defined as [] (i.e. all of its dimensions are 0) may give you different results than an empty matrix that still has some non-zero dimensions. For example, matrix multiplication can give you different (and somewhat non-intuitive) results when dealing with different kinds of empty matrices. Let's consider these 3 empty matrices:

>> a = zeros(1,0);  %# A 1-by-0 empty matrix
>> b = zeros(0,1);  %# A 0-by-1 empty matrix
>> c = [];          %# A 0-by-0 empty matrix

现在,让我们尝试以不同的方式将它们相乘:

Now, let's try multiplying these together in different ways:

>> b*a

ans =
     []  %# We get a 0-by-0 empty matrix. OK, makes sense.

>> a*b

ans =
     0   %# We get a 1-by-1 matrix of zeroes! Wah?!

>> a*c

ans =
   Empty matrix: 1-by-0  %# We get back the same empty matrix as a.

>> c*b

ans =
   Empty matrix: 0-by-1  %# We get back the same empty matrix as b.

>> b*c
??? Error using ==> mtimes
Inner matrix dimensions must agree.  %# The second dimension of the first
                                     %#   argument has to match the first
                                     %#   dimension of the second argument
                                     %#   when multiplying matrices.

通过将两个空矩阵相乘来获得一个非空矩阵可能足以使您的头部受伤,但是这是有道理的,因为结果仍然不包含任何东西(即它的值为0).

Getting a non-empty matrix by multiplying two empty matrices is probably enough to make your head hurt, but it kinda makes sense since the result still doesn't really contain anything (i.e. it has a value of 0).

这篇关于MATLAB中[]和[1x0]之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆