【问题标题】:matlab-return matrixmatlab-返回矩阵
【发布时间】:2012-11-03 05:48:31
【问题描述】:

我需要帮助,如何从 matlab 中的函数返回矩阵? 我有一个零矩阵(大小 NxN)。我将矩阵发送到某个函数以更新她。 如何返回更新后的矩阵?

在代码中:

matrix = zeros(size); %put zeros
updateMatrix(radius,x0,y0,matrix);%call to function

function updateMatrix(radius,x0,y0,matrix)
    update the matrix
end

continue the prog with the updated matrix

我只需要返回更新后的矩阵,其他变量不用修改。

我尝试过这样做:

matrix = zeros(size); %put zeros
matrix=updateMatrix(radius,x0,y0,matrix);%call to function

function [matrix]=updateMatrix(radius,x0,y0,matrix)
    update the matrix
end

continue the prog with the updated matrix

但它不起作用。

谢谢!

【问题讨论】:

    标签: arrays matlab matrix return


    【解决方案1】:

    Matlab 不支持指针,除非指定,否则不能更改输入。试试这样的。

    matrix=updateMatrix(radius,x0,y0,matrix)    
    
    function matrix=updateMatrix(radius,x0,y0,matrix)
        %update the matrix
    end
    

    【讨论】:

    • “赞”按钮在哪里?效果很好!非常感谢!
    • @shlomi,使用“upvote”按钮和“accept”按钮而不是like
    【解决方案2】:

    您不能像在 C 或 C++(或任何数量的其他语言)中那样传递指向 MATLAB 函数的指针或引用,并让它对数据进行就地操作。但是,MATLAB 优化器应该能够识别意图在函数内就地改变数据的情况。 This optimization 是几年前添加的。

    把你的函数写成

    function matrix = updateMatrix( radius, x0, y0, matrix )
      % do whatever to the matrix variable
    end
    

    称之为

    m = zeros( row, col );
    m = updateMatrix( r, x0, y0, m );
    

    诀窍是保持输入和输出变量的名称相同,以便优化器意识到您希望就地改变数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 2011-12-03
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      相关资源
      最近更新 更多