矩阵作为函数的输出

发布于 2024-08-19 21:10:25 字数 593 浏览 4 评论 0原文

也许是一个非常简单的问题,但我已经在互联网上寻找了几个小时的答案,但我找不到它。

我创建了下面的函数。在另一个 m 文件中,我想使用矩阵“actual_location”。然而,不可能使用矩阵的单个单元(即actual_location(3,45) 或actual_location(1,2))。当我尝试使用单个单元格时,出现以下错误:???使用 ==> 时出错实际位置 输入参数太多。

谁能告诉我必须更改什么,以便我可以读取矩阵的各个单元格?

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

Maybe a very easy question, but I am already looking for hours on the Internet for the answer but I cannot find it.

I have created the function below. In another m-file I want to use the matrix 'actual_location'. However, it is not possible to use an individual cell of the matrix (i.e. actual_location(3,45) or actual_location(1,2)). When I try to use an individual cell, I get the following error : ??? Error using ==> Actual_Location
Too many input arguments.

Can anyone please tell me what I have to change, so that I can read individual cells of the matrix?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉睡月亮 2024-08-26 21:10:25

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

actual_location = Actual_Location(arguments);

你只是为了迷惑自己而写的。在函数定义中使用实际_位置以外的名称作为虚拟参数,并调用该函数以返回具有更独特名称的变量,如下所示:

output = Actual_Location(arguments);

看来您可能期望实际_位置(1,1)返回元素1,1 是一个数组,而它可能是一个带有 2 个输入参数的函数调用。

As you have defined it, the name in the m-file for the matrix written by your function Actual_Location is actual_location. However, when you call your function you can give the output any name you like. I presume that you are calling it like this, remembering that Matlab is a bit case-sensitive:

actual_location = Actual_Location(arguments);

You are just writing to confuse yourself. Use a name other than actual_location for the dummy argument in the function definition, and call the function to return to a variable with a more distinct name, something like this:

output = Actual_Location(arguments);

It appears that you may be expecting actual_location(1,1) to return element 1,1 of an array, whereas it is, probably, a function call with 2 input arguments.

段念尘 2024-08-26 21:10:25

这似乎表明您正在使用许多参数调用 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

This seems to suggest you are calling the Actual_Location function with to many arguments... I'm re-writing your code with proper indentation.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文