필터 지우기
필터 지우기

Problem with iteration involving matrix

조회 수: 1 (최근 30일)
muhammad muda
muhammad muda 2019년 5월 28일
댓글: muhammad muda 2019년 5월 28일
Hi,
I have problem in my coding that you can see in the attachment. Initially I have 4x4 matrix and I am computing the distance between each rows to find the compatibility C.
Everything seems good in the first iteration (for jj = 1:1). I have 4x4 matrix for C.
In second iteration (for jj = 1:2) the number of rows has been reduced to 3, hence I should be getting 3x3 matrix for C. But, I still get 4x4 matrix.
Please help me. Thank you.
Mmax = [8 2 3 9; 7 3 5 9; 6 4 7 10; 5 1 8 7]; %setting the initial matrix Mmin and Mmax
Mmin = Mmax;
for jj = 1:2 % jj = 1:1 for only 1 iteration
for i = 1:size(Mmax,1)
for j = 1:size(Mmin,1)
A = Mmax(i,:)
B = Mmax(j,:)
Y = Mmin(i,:)
Z = Mmin(j,:)
length = 0;
for k = 1:4
length = length + (max(A(k),B(k)) - min(Y(k), Z(k)));
end
length = length/4;
C(i,j) = 8 - length; %after 2nd iteration, i and j are both 3. But why do I have 4x4 matrix for C?
end
end
C = C - diag(diag(C));
maximum = max(max(C));
[x,y] = find(C==maximum);
%start merging
R1 = Mmax(x(1),:);
R2 = Mmin(y(1),:);
Rmax = max(R1,R2);
Rmin = min(R1,R2);
Mmax(x(1),:) = [];
Mmax(y(1),:) = [];
Mmin(x(1),:) = [];
Mmin(y(1),:) = [];
Mmax = [Mmax(1:end,:); Rmax]
Mmin = [Mmin(1:end,:); Rmin]
end

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 5월 28일
편집: KALYAN ACHARJYA 2019년 5월 28일
Why you expect that when jj=2 the C should be 3x3?
No
Your code is independent with jj, have you seen anywhere that apart from following line
for jj=1:2
jj just use to interarte the code two times, rest part of the code jj has no role.
Hence irrespective of any value of jj, you will get the following same output
The interation number 1
0 7.0000 5.7500 5.2500
7.0000 0 6.7500 5.7500
5.7500 6.7500 0 6.0000
5.2500 5.7500 6.0000 0
The interation number 2
0 6.0000 5.7500 5.2500
6.0000 0 5.0000 5.7500
5.7500 5.0000 0 6.0000
5.2500 5.7500 6.0000 0
  댓글 수: 1
muhammad muda
muhammad muda 2019년 5월 28일
Actually I do expect the C to be 3x3 because after first iteration, the matrix Mmin and Mmax is 3x4, reduced one row than before.
The values of i and j are also 3. So C (i,j)should be 3x3. Maybe I'm wrong but that is what I intend to do. I'm really stuck here.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by