Build a relation between matrix components
显示 更早的评论
Hi, I wanted to ask whether there is an easier method to calculate the sum of relations for each component by considering the neghborhood of each point of a matrix. For example, by considering a 3*3 matrix:
a=[a11,a12,a13; a21,a22,a23; a31,a32,a33]
I want to calculate the sum of relations, in which the relation is:
r=(a-b)/(a+b)
So for example, for component a11, I would have:
r11=(a11-a12)/(a11+a12) + (a11-a22)/(a11+a22) + (a11-a21)/(a11+a21)
Essentially I want to calculate the sum of relations around each point. For example, for a11, there are 3 other points, so I have 3 relations summed up; for another point, for example, point a22, I have 8 other points in the neighborhood of a22, so I would have 8 relations that would be summed up. I've written a function for it but it's very long. A small portion of it:
function [c] = relation(y, i, j, m, n)
c=0;
if i~=1 && j~=1 && i~=m && j~=n
c = (y(i,j) - y(i,j+1))/ (y(i,j) + y(i,j+1));
c = c + (y(i,j) - y(i+1,j+1)) / (y(i,j) + y(i+1,j+1));
c = c + (y(i,j) - y(i+1,j))/ (y(i,j) + y(i+1,j));
c = c + (y(i,j) - y(i+1,j-1))/ (y(i,j) + y(i+1,j-1));
c = c + (y(i,j) - y(i-1,j-1))/ (y(i,j) + y(i-1,j-1));
c = c + (y(i,j) - y(i-1,j))/ (y(i,j) + y(i-1,j));
c = c + (y(i,j) - y(i-1,j+1))/ (y(i,j) + y(i-1,j+1));
c = c + (y(i,j) - y(i,j-1))/ (y(i,j) + y(i,j-1));
end
if i==1 && j==1
c = (y(i,j) - y(i,j+1))/ (y(i,j) + y(i,j+1));
c = c + (y(i,j) - y(i+1,j+1)) / (y(i,j) + y(i+1,j+1));
c = c + (y(i,j) - y(i+1,j))/ (y(i,j) + y(i+1,j));
end
.
.
.
I wanted to ask whether there would be an easier way to calculate the sum of relations.
Thank you.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Performance and Memory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!