0
点赞
收藏
分享

微信扫一扫

190320-Matlab矩阵逐行进行函数运算

船长_Kevin 2023-03-03 阅读 93


​​how to apply a function to all rows in a matrix? [duplicate]​​

tmp.m

  • Example1

A = reshape(1:9,3,3)

arrayfun(@(n) myfun(A(n,:)), 1:size(A,1))

function [b] = myfun(data)
b = data(1)+2*data(2)+3*data(3);
end

>> tmp

A =

1 4 7
2 5 8
3 6 9


ans =

30 36 42

  • Example2

A = reshape(1:9,3,3)

b = arrayfun(@(n) myfun1(A(n,:)), 1:size(A,1),'UniformOutput', false)

B = cell2mat(b)

function [c] = myfun1(data1)
c = myfun2(data1);
end


function [d] = myfun2(data2)
d =power(data2,2);
end

>> tmp

A =

1 4 7
2 5 8
3 6 9


b =

1×3 cell array

{1×3 double} {1×3 double} {1×3 double}


B =

1 16 49 4 25 64 9 36 81


举报

相关推荐

0 条评论