0
votes

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
1
Just curious if the answer provided here worked for you.Divakar

1 Answers

0
votes

Code

table1 = [
    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];

table2=NaN(size(table1));
for k1 = 1:size(table1,1)
    for k2 = 1:size(table1,2)
        rows_cat = [table1(k1,:) ; table1(k2,:)];
        table2(k1,k2) = sum(min(rows_cat));
    end
end

Output

table2 =
    22     6     7     1     6
     6     6     0     0     0
     7     0    10     4     0
     1     0     4     6     2
     6     0     0     2     8

No-Loop version

%%// Generate combinations
vectors = {1:5,1:5};
combs = combvec(vectors{:}).'; %%//'

t2 = reshape(combs',[],1); %//'
t4 = reshape(table1(t2,:)',5,2,[]);  %//'
table2 = reshape(squeeze(sum(min(t4,[],2))),5,5)

The combinations code snippet was taken from here.