1
votes

Consider matrix1 as m1 and matrix2 as m2.So below are the operations to obtain m2.m1 is column matrix.m2 is mxn matrix.In my example m1 is 5x1 matrix and m2 is 5x5 matrix.


Operation to obtain diagonal elements of m2:

m2(1,1)=m1(1,1)

m2(2,2)=m1(2,1)

m2(3,3)=m1(3,1)

m2(4,4)=m1(4,1)

m2(5,5)=m1(5,1)

Operation to obtain other elements of m2:

Equation:r(i,j)=min(p(i,k),q(j,k)) where k=1

m2(1,2)=min(m1(1,1),m1(2,1))

m2(1,3)=min(m1(1,1),m1(3,1))

m2(1,4)=min(m1(1,1),m1(4,1))

m2(1,5)=min(m1(1,1),m1(5,1))

...

...

Source Code I tried:

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
            minval = minval + min(a(i,k),a(j,k));
            end
            B(i,j)= minval;
        end
    end
end    

This code is working fine for below input matrix:

22
 6
10
 6
 8

Output matrix i got for this code:

22     6    10     6     8
 6     6     6     6     6
10     6    10     6     8
 6     6     6     6     6
 8     6     8     6     8

But the code is not working for below input matrix:

 610
 442
 699
 464
 774

Output matrix iam getting for above input matrix:

matrix of 5 rows and 49 columns.

But expected output matrix:

 610  442  610  464  610
 442  442  442  442  442 
 610  442  699  464  699 
 464  442  464  464  464
 610  442  699  464  774

What is the solution?

1

1 Answers

1
votes

Try this bsxfun one-liner that seems to take care of all your needs -

m2 = bsxfun(@min,m1,permute(m1,[3 1 2]))