MATLAB Answers

manipulation attribute between two matrices inside loop

조회 수: 2(최근 30일)
Khairul Nur
Khairul Nur 15 Nov 2019
Commented: Adam Danz 16 Nov 2019
Hi, im new in matlab. Please help me to manipulate 2 matrix. For example if i have matrix MM(40X10) and MMM(4X10). I also need to do some calculate on each attribute of the matrix. For example :
MM = %example should have 40 in rows
13 25 15 11 11 14 12 12 10 14
14 25 15 11 11 14 12 12 10 14
14 25 15 11 11 14 12 12 10 14
13 25 15 12 11 14 12 12 10 15
13 25 15 12 11 14 12 12 10 15
MMM =
13 25 14 11 11 13 12 12 12 13
13 25 14 11 11 13 12 12 12 13
13 24 14 11 11 14 12 12 12 14
15 25 15 11 11 14 12 12 10 14
each row in MMM should minus with MM , take the sum and square by 2 , should be :
MM row 1 compute with MMM row1
MM row 1 compute with MMM row2
MM row 1 compute with MMM row3
MM row 1 compute with MMM row4
next loop
MM row 2 compute with MMM row1
MM row 2 compute with MMM row2
MM will execute until 40 times
here is my current code:
for n=i:40
for nn=j:4
result1 = sum((MMM(i,j)- MM(i,:)).^2)
end
end
however, matlab do not turn any error and also any result. Please help me.

  댓글 수: 3

Adam Danz
Adam Danz 15 Nov 2019
What is i and j?
Your loops are set incorrectly. i and j are always the same value. That's not how you use a for-loop.
You're also overwriting the result on each iteration.
Take some time to understand how for-loops are used
Khairul Nur
Khairul Nur 15 Nov 2019
i is 1 maybe can put 1:40
Adam Danz
Adam Danz 16 Nov 2019
But that's not the only problem. The loops should look something like this.
for n=1:40
for nn=1:4
result1 = sum((MMM(n, nn)- MM(n,:)).^2)
end
end

로그인 to comment.

답변 수 (1)

KSSV
KSSV 15 Nov 2019
If you are new to MATLAB, read about basics of MATLAB. Read about matrix indexing and loops.
MM = rand(40,10) ;
MMM = rand(4,10) ;
R = zeros(4,10) ;
for i = 1:4
R(i,:) = sum((MMM(i,:)-MM).^2) ;
end

  댓글 수: 1

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by