# Matrix formation by applying some operation using loop logic.

4 views (last 30 days)
prashanth on 23 Apr 2014
Commented: Chandrasekhar on 23 Apr 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

Chandrasekhar on 23 Apr 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
prashanth on 23 Apr 2014
thanx thanx thank you so much.

Andrei Bobrov on 23 Apr 2014
Edited: Andrei Bobrov on 23 Apr 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);
Chandrasekhar on 23 Apr 2014
excellent..Really simple.. thanks
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.mathworks.com/matlabcentral/answers/126765-matrix-formation-by-applying-some-operation-using-loop-logic", "fetch_time": 1642708356000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2022-05/segments/1642320302622.39/warc/CC-MAIN-20220120190514-20220120220514-00068.warc.gz", "warc_record_offset": 834254958, "warc_record_length": 25050, "token_count": 751, "char_count": 1624, "score": 3.6875, "int_score": 4, "crawl": "CC-MAIN-2022-05", "snapshot_type": "latest", "language": "en", "language_score": 0.7486690282821655, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00047-of-00064.parquet"}