Element wise operation with own function without loops

40 ビュー (過去 30 日間)
Radmilka
Radmilka 2015 年 5 月 7 日
コメント済み: Radmilka 2015 年 5 月 7 日
Hello! What is the syntax to do element wise operation with own function? Say, I have matrix A=magic(5) and function myFun(x). And need to do
for i=1:size(A,1)
for j=1:size(A,2)
A2=myFun(A(i,j));
end
end
How to avoid looping?
And one more question. I have an array F or 10 different matrices f1, f2... f10 and my function
function y = Q( f,h,g )
y=En(conv2(f,h,'full')-g)/En(g);
end
How can I apply this function to each matrix of this array simultaneously and without loops?

採用された回答

Guillaume
Guillaume 2015 年 5 月 7 日
First question, use arrayfun:
A2 = arrayfun(@myfunc, A);
Second question, assuming your 10 different matrices are held in cell array F, use cellfun:
h = ??? %something
g = ??? %something else
F2 = cellfun(@(f) Q(f, h, g), F, 'UniformOutput', false); %assuming that Q returns non-scalar.
  1 件のコメント
Radmilka
Radmilka 2015 年 5 月 7 日
I'll try this :) Thanks a lot for fast reply!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by