Problem with matrices operations
3 次查看(过去 30 天)
显示 更早的评论
My Matlab knowledge is minimal and I was hoping someone could help me. I have generated two 4x900 size matrices. The matrices are X and Y. The operation that I must carry out is the following:
N=900
R(1)=(X(1st column)-Y(1st column))*transpose(X(1st column)-Y(1st column))
R(2)=(X(2nd column)-Y(2nd column))*transpose(X(2nd column)-Y(2nd column))
% ...
R(N)=(X(Nth column)-Y(Nth column))*transpose(X(Nth column)-Y(Nth column))
R_total=sum(R(1)+R(2)+...+R(N))
As an example, it would be something like this:
N=5;
X = [1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5];
Y = [5 6 7 8 9; 5 6 7 8 9; 5 6 7 8 9; 5 6 7 8 9];
R(1)=([1;1;1;1]-[5;5;5;5])*transpose([1;1;1;1]-[5;5;5;5]);
R(2)=([2;2;2;2]-[6;6;6;6])*transpose([2;2;2;2]-[6;6;6;6]);
% ...
R(5)=([5;5;5;5]-[9;9;9;9])*transpose([5;5;5;5]-[9;9;9;9])
R_total=sum(R(1)+R(2)+...+R(N));
The matrices R(1), R(2), ..., R(N) y R_total must have a size of 4x4.
I have been trying to use a 'while', but I don't know how to perform the operation. Does anyone know how I could solve it? I know I have comitted some errors calling R(1), R(2)...R(N).
Sorry if the process is not clear. I have tried to explain the operations as well as I could. If someone needs any extra information, please, tell me.
Thank you!
0 个评论
采纳的回答
Sriram Tadavarty
2020-3-22
编辑:Sriram Tadavarty
2020-3-23
Hi,
You can try the following:
x = rand(4,900);
y = rand(4,900);
sumOut = zeros(4,4); % Updated variable name to make it different from inbuilt function
for i = 1:900
tmp = x(:,i)-y(:,i);
R(:,:,i) = tmp*tmp';
sumOut = sumOut + R(:,:,i);
end
Hope this helps.
Regards,
Sriram
2 个评论
Stephen23
2020-3-22
Naming a variable sum is not recommended because this shadows the inbuilt sum function.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fourier Analysis and Filtering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!