Problem with matrices operations

조회 수: 13 (최근 30일)
Daniel Álvarez
Daniel Álvarez 2020년 3월 22일
편집: Sriram Tadavarty 2020년 3월 23일
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!

채택된 답변

Sriram Tadavarty
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
Stephen23 2020년 3월 22일
Naming a variable sum is not recommended because this shadows the inbuilt sum function.
Sriram Tadavarty
Sriram Tadavarty 2020년 3월 23일
Yes. you are right. Updated the variable with other name,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by