How to use a formula for all elements of matrix?

8 次查看(过去 30 天)
I have a matrix the
Mij =[ NaN 2.5000 0
2.5000 NaN -0.8333
0 -0.8333 NaN]
And I have a general formula::
phi=atand(abs((m2-m1)/(1+m1*m2)))
I need to use this formula for all elements of my matrix.
For example, I want to use as m2=M12(element in first row and second column)and
as m1=M32 (element in third row and second column). and according the formula
phi=atand(abs((Mij(1,2)-Mij(3,2))/(1+Mij(3,2)*Mij(1,2)));
And I need for all cases, which for all elements in matrix.
Can anyone help me?
I have tried as below, but the result is wrong:
for u=1:E1
for v=1:E1
p(v,u)=abs((Mij(v,u)-Mij(v,u))/(1+Mij(v,u)*Mij(v,u)));
end
end
Result is wrong:
p =
NaN 0 0
0 NaN 0
0 0 NaN

采纳的回答

Torsten
Torsten 2016-7-19
for u1=1:size(M,1)
for v1=1:size(M,2)
m1 = M(u1,v1);
for u2=1:size(M,1)
for v2=1:size(M,2)
m2 = M(u2,v2);
phi(u1,v1,u2,v2)=atand(abs((m1-m2)/(1+m1*m2)));
end
end
end
end
Best wishes
Torsten.

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2016-7-19
M=[NaN 2.5000 0
2.5000 NaN -0.8333
0 -0.8333 NaN];
phi=@(m1,m2)atand(abs((m2-m1)./(1+m1.*m2)));
out1 = bsxfun(phi,M(:),M(:)');
out2 = reshape(out1,[size(M),size(M)]);

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by