size()返回1,其中矩阵维不应该存在 [英] size() returns 1 where matrix dimension should not exist

查看:82
本文介绍了size()返回1,其中矩阵维不应该存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我键入X = rand(2,3),则size(X,1)size(X,2)会产生预期的结果.如果键入ndims (X),则可以得到预期的二维.

If I type X = rand(2,3) then size(X,1) and size(X,2) produce the expected results. If I type ndims (X) I get the expected two dimensions.

但是size(X, k) == 1,其中k是任何整数>3.为什么会发生这种情况?

However, size(X, k) == 1, where k is any integer > 3. Why does this occur?

推荐答案

如果大小的行为要保持一致,则必须发生这种情况.我们认为向量的大小为1xn或nx1.当然方向很重要,否则MATLAB可以简单地告诉您向量的长度为n.在后一种情况下,存在尾随的单例维,因此隐式地存在无限多个尾随的单例维.所以那个列向量的大小实际上是nx1x1x1 ...,而行向量的大小是1xnx1x1 ...

This must happen, if the behavior of size is to be consistent. We think of a vector as of size 1xn or nx1. Of course the orientation is important, else MATLAB could simply tell you the vector is of length n. In the latter case, there is a trailing singleton dimension, so implicitly there are infinitely many trailing singleton dimensions. So that column vector is really of size nx1x1x1..., and a row vector is of size 1xnx1x1...

标量也是如此.如果标量的大小为1x1,那么它的大小一定为1x1x1x1x ...

The same is true of a scalar. If the scalar is of size 1x1, then it must really be of size 1x1x1x1x...

大小必须告诉您对象的大小,并且应该在哪里停止?我想如果您要从头开始重新设计MATLAB,您可能会决定不报告任何尾随的单例尺寸.因此,列向量的大小为n,但是行向量的大小为1xn?老实说,我实际上更喜欢被告知列向量的维数为nx1.但是从逻辑上讲,它的尺寸必须为nx1x1x1 ...

Size must tell you the size of an object though, and where should it stop? I suppose if you were to redesign MATLAB from scratch, you might decide to not report any trailing singleton dimensions. So a column vector would be reported as of dimension n, but a row vector of dimension 1xn? To be honest, I actually prefer being told a column vector has dimension nx1. But then it must logically be of dimension nx1x1x1...

无论如何,很久以前,MATLAB不允许您为数组中的二维索引.高维数组在这里已经存在了至少20年左右,但是仍然有许多旧代码仍然有用. (我有25年前编写的代码,现在仍在使用.)您不想破坏现有的代码.

Anyway, long ago MATLAB did not allow you to index beyond two dimensions in arrays. Higher dimensional arrays have been here for at least 20 years or so, but there is still much legacy code that is still useful. (I've got code that I wrote over 25 years ago and is still in use.) You don't want to break existing code.

这篇关于size()返回1,其中矩阵维不应该存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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