Write Similar Expressions in Arrayfun

Is there a concise way to write these 4 expressions so that I don't have to write all of them seperately mayble in a for loop?
n= numel(X);
r_x=1./X; % (X, Y , Z , R are square matrices)
t_x = @(x) sum((x-r_x)./(x+r_x),'all')/n;
x = arrayfun(t_x,r_x);
r_y=1./Y;
t_y = @(x) sum((x-r__y)./(x+r_y),'all')/n;
y = arrayfun(t_y,r_y);
r_z=1./Z;
t_z = @(x) sum((x-r_z)./(x+r_z),'all')/n;
z = arrayfun(t_z,r_z);
r_r=1./R;
t_r = @(x) sum((x-r_r)./(x+r_r),'all')/n;
r = arrayfun(t_r,r_r);

2 件のコメント

Matt J
Matt J 2022 年 1 月 3 日
How big are X,Y,Z, and R?
MarshallSc
MarshallSc 2022 年 1 月 3 日
100 * 100. I have 20 matrices that I need to perform this operation on.

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

 採用された回答

Matt J
Matt J 2022 年 1 月 3 日
編集済み: Matt J 2022 年 1 月 3 日

1 投票

n=numel(X);
r=1./[X(:),Y(:),Z(:),R(:)].';
rt=reshape(r,4,1,[]);
out= squeeze( sum( (rt-r)./(rt+r) ,2)/n ).';
out=num2cell( reshape( out , [size(X),4] ) ,[1,2]);
[x,y,z,r]=deal(out{:});

3 件のコメント

MarshallSc
MarshallSc 2022 年 1 月 3 日
編集済み: MarshallSc 2022 年 1 月 3 日
Thanks Matt. I receive this error:
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for
that dimension.
Error in
out=num2cell(reshape(out , [size(X),4]) ,[1,2]);
Matt J
Matt J 2022 年 1 月 3 日
Sorry, try it now.
MarshallSc
MarshallSc 2022 年 1 月 3 日
Thanks Matt!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2022 年 1 月 3 日

コメント済み:

2022 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by