了解为什么我不能在Matlab中的矩阵上的大小函数的结果上使用索引 [英] Understanding why I can't use index on the result of the size function on a matrix in matlab
本文介绍了了解为什么我不能在Matlab中的矩阵上的大小函数的结果上使用索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试时:
a = [ 1 1 ; 1 1 ];
size(a)(1)
我知道
Error: ()-indexing must appear last in an index
expression
我的理解是size(a)的结果是大小为1 2的矩阵.
My understanding is that the result of size(a) is a matrix of size 1 2.
问题:为什么size(a)(1)不起作用?
Question: Why does size(a)(1) not work?
致敬.
推荐答案
因为在不创建临时函数的情况下,无法在MATLAB中为类似函数的结果建立索引.
Because you can't index the result of a function like that in MATLAB without creating a temporary.
temp = size(a);
temp(1)
将起作用.但是,通常还有其他获取您想要的东西的方法.在您的示例中,您可以在函数size(X,dim)
中使用dim
参数:
will work. There are often other ways of getting what you want, however. In your example, you can make use of the dim
argument in the function size(X,dim)
:
size(a,1)
这将直接为您提供第一维的大小,而无需创建临时变量.
That will get you the size of the first dimension directly, avoiding the need to create a temporary variable.
这篇关于了解为什么我不能在Matlab中的矩阵上的大小函数的结果上使用索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文