2

考虑以下矩阵:

a=[1,2,3]

所以

size(a)=[1,3]

我想将第二维 3 分配给变量 n。最有效的方法是什么?

为什么以下不起作用?

[[],n]=size(a)

或者

  n= num2cell(size(a)){2}
4

1 回答 1

2

This is probably the simplest, and works for a with any number of dimensions:

n = size(a,2);

If a is guaranteed to have exactly 2 dimensions, you could also use

[ m, n ] = size(a);

and if you don't need the first variable, in recent versions of Matlab you can write

[ ~, n ] = size(a);

As for the things you have tried:

  • [[],n]=size(a) does not work because [] is not a variable to which you can assign anything.

  • n= num2cell(size(a)){2} does not work because you can't directly index like that in Matlab. You would need a temporary variable: temp = num2cell(size(a)); n=temp{2}. Or dispose of num2cell and do: temp = size(a); n=temp(2).

于 2013-08-15T23:22:44.883 回答