2010-02-01 79 views
5

也许一个非常简单的问题,但我已经在Internet上寻找答案的时间了,但我找不到它。矩阵作为函数的输出

我已经创建了下面的函数。在另一个m文件中,我想使用矩阵'actual_location'。但是,不可能使用矩阵的单个单元(即actual_location(3,45)或actual_location(1,2))。当我尝试使用单个单元格时,出现以下错误:??? Error using ==> Actual_Location Too many input arguments.

任何人都可以告诉我必须更改哪些内容,以便可以读取矩阵的单个单元格?

function [actual_location] = Actual_Location(~); 
actual_location=zeros(11,161); 
for b=1:11 
    for t=1:161 
     actual_location(b,t) = (59/50)*(t-2-(b-1)*12)+1; 
     if actual_location(b,t) < 1 
      actual_location(b,t) =1; 
     end  
    end 
    actual_location(1,1) 
end 
+0

你在哪里/如何使用actual_location? – 2010-02-01 17:27:37

+0

我在其他m文件中使用actual_location进行一些简单的计算。 (添加和删除) 我使用下面的代码在另一个m文件中打开它: 'Actual_Location' – Daan 2010-02-01 17:33:23

回答

1

正如您所定义的,您的函数Actual_Location所写的矩阵的m文件中的名称是actual_location。但是,当你调用你的函数时,你可以给你输出任何你喜欢的名字。我相信,你是这样的调用它,记住,Matlab是有点区分大小写:

actual_location = Actual_Location(arguments); 

你只是写来迷惑自己。使用其他名称超过actual_location在函数定义的伪参数,并调用该函数返回一个变量,更鲜明的名字,像这样:

output = Actual_Location(arguments); 

看来,你可能会希望actual_location( 1,1)返回一个数组的元素1,1,而它可能是一个带有2个输入参数的函数调用。

+0

对于函数的名称,matlab不区分大小写。因此,如果有一个名为'foobar'的函数,可以称之为'fOoBaR'或'FOObAr'等,一旦调用函数称为Actual_Location,matlab解释器将解释actual_location(.. 。)'作为对该函数的调用,导致出现错误。对于使用'()'进行矩阵索引的M/W蒙羞。 – shabbychef 2010-02-01 18:19:01

+2

Matlab是排序区分大小写的。它会首先查找完全匹配,然后检查不完全匹配(现在抛出一个警告,但在将来的版本中出现错误)。 此外,矩阵索引的()是一致的,因为访问一个矩阵正在调用一个可以被重载的函数(subsref,subsasgn)。 – Jonas 2010-02-01 18:46:53

1

这似乎表明你正在调用Actual_Location函数以多种参数...我正在用适当的缩进重写你的代码。

function [actual_location] = Actual_Location() 
    actual_location=zeros(11,161); 
    for b=1:11 
    for t=1:161 
     actual_location(b,t) = (59/50)*(t-2-(b-1)*12)+1; 
     if actual_location(b,t) < 1 
     actual_location(b,t) = 1; 
     end 
    end 
    actual_location(1,1) 
    end