Matrix formation by applying some operation using loop logic.
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
prashanth
le 23 Avr 2014
Commenté : Chandrasekhar
le 23 Avr 2014
This first matrix table1 contains 5 names and 5 scenes.I need to perform some operations on this matrix and I have to obtain second matrix as shown in table2.
Diagonal elements of table2 should obtained by performing addition of 1st row,2nd row,3rd row..and so on.Remaining elements obtained by comparing 2 rows and summing them.Suppose take table1 as A matrix and table2 as B matrix.
OPERATION1:For diagonal elements
B(1,1)=7+7+7+1+0=22
B(2,2)=6+0+0+0+0=6
B(3,3)=0+6+0+4+0=10…….and so on
OPERATION2:For remaining elements
B(1,2)=MIN(A(1,1),A(2,1))+ MIN(A(1,2),A(2,2))+ MIN(A(1,4),A(2,4))+ MIN(A(1,5),A(2,5));
B(1,3)=
B(1,4)=
B(1,5)=
Table1:
Scene1 Scene2 Scene3 Scene4 Scene5
BASAVARAJ 7 7 7 1 0
MANOJ 6 0 0 0 0
NATESH 0 6 0 4 0
VIJAY 0 0 0 4 2
GOWDA 0 0 6 0 2
Table2:
BASAVARAJ MANOJ NATESH VIJAY GOWDA
BASAVARAJ 22 6 7 1 6
MANOJ 6 6 0 0 0
NATESH 7 0 10 4 0
VIJAY 1 0 4 6 2
GOWDA 6 0 0 2 8
0 commentaires
Réponse acceptée
Chandrasekhar
le 23 Avr 2014
A = [7 7 7 1 0;6 0 0 0 0; 0 6 0 4 0;0 0 0 4 2;0 0 6 0 2];
for i = 1:5
for j = 1:5
if i == j
B(i,j) = sum(A(i,1:end));
else
minval = 0;
for k = 1:5
minval = minval + min(A(i,k),A(j,k));
end
B(i,j)= minval;
end
end
end
Plus de réponses (1)
Andrei Bobrov
le 23 Avr 2014
Modifié(e) : Andrei Bobrov
le 23 Avr 2014
s = size(A);
b = nchoosek(1:s(1),2);
B = zeros(s);
B(tril(true(s),-1)) = sum(min(A(b(:,1),:),A(b(:,2),:)),2);
B = B + B.';
B(eye(s)>0) = sum(A,2);
1 commentaire
Voir également
Catégories
En savoir plus sur Elementary Math dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!