Given a matrix A, create the matrix B whose elements are the neighbor sumr for A

조회 수: 2 (최근 30일)
I'm havig trouble on matrix B, I should get B = [7 10 13 11; 16 24 28 23; 28 40 44 35; 23 28 41 27]
but, I'm getting B =
13 13 13 23
16 24 28 20
28 40 44 32
0 38 41 27
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
C = size(A)
M = C(1)
N = C(2)
% Step 1: Get elements of the first row of matrix B from matrix A:
B(1,1) = A(1,2) + A(2,1)
for j = 2:N-1
B(1:j) = A(1,j-1) + A(2,j) + A(1,j+1)
end
B(1,N) = A(1,N-1) + A(2,N)
% Step 2: Get elements of the middle rows of Matrix B from matrix A:
for i = 2:M-1
B(i,1) = A(i-1,1) + A(i,2) + A(i+1,1)
end
for j = 2:N-1
for i = 2:M-1
B(i,j) = A(i,j-1) + A(i-1,j) + A(i+1,j) + A(i,j+1)
end
end
for i = 2:M-1
B(i,N) = A(i,j-1) + A(i-1,j) + A(i+1,j)
end
% Step 3: Get elements of the last row of matrix B from matrix A:
B(1,N) = A(N-1,1) + A(N,2)
for j = 2:N-1
B(N,j) = A(N,j-1) + A(N-1,j) + A(N,j+1)
end
B(M,N) = A(M,N-1) + A(M-1,N)
disp(B)

채택된 답변

Debasish Samal
Debasish Samal 2019년 6월 17일
There are multiple indexing errors in your code Jose. Here is the rectified version.
A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
C = size(A)
M = C(1)
N = C(2)
% Step 1: Get elements of the first row of matrix B from matrix A:
B(1,1) = A(1,2) + A(2,1)
for j = 2:N-1
B(1,j) = A(1,j-1) + A(2,j) + A(1,j+1)
end
B(1,N) = A(1,N-1) + A(2,N)
% Step 2: Get elements of the middle rows of Matrix B from matrix A:
for i = 2:M-1
B(i,1) = A(i-1,1) + A(i,2) + A(i+1,1)
end
for j = 2:N-1
for i = 2:M-1
B(i,j) = A(i,j-1) + A(i-1,j) + A(i+1,j) + A(i,j+1)
end
end
for i = 2:M-1
B(i,N) = A(i,N-1) + A(i-1,N) + A(i+1,N)
end
% % Step 3: Get elements of the last row of matrix B from matrix A:
B(N,1) = A(N-1,1) + A(N,2)
for j = 2:N-1
B(N,j) = A(N,j-1) + A(N-1,j) + A(N,j+1)
end
B(M,N) = A(M,N-1) + A(M-1,N)
disp(B)

추가 답변 (2개)

Stephen23
Stephen23 2019년 6월 17일
편집: Stephen23 2019년 6월 17일
Much simpler to just use conv2:
>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> B = [7 10 13 11; 16 24 28 23; 28 40 44 35; 23 28 41 27]
B =
7 10 13 11
16 24 28 23
28 40 44 35
23 28 41 27
>> conv2(A,[0,1,0;1,0,1;0,1,0],'same')
ans =
7 10 13 11
16 24 28 23
28 40 44 35
23 38 41 27
Your example B matrix has a mistake in the last row.

Navdha Agarwal
Navdha Agarwal 2019년 6월 17일
Hi Jose,
As I understand from your question, you want to create a matrix B from a matrix A such that the elements of matrix B are the sum of intermediate neighbours of the corresponding element of matrix A.
So the final matrix B should be :
[7 10 13 11;
16 24 28 23;
28 40 44 35;
23 38 41 27]
Note: I think the 2nd element in the last row should be 38.
I have found out three errors in the code mentioned above:
  1. The third line in step 1 should be :
B(1,j) = A(1,j-1) + A(2,j) + A(1,j+1)
There should be a comma in place of : (colon) in the indexing.
2. In step 2, when you are updating the last element of the middle rows, the value j should have been incremented by one before the start of this loop.
j = j+1
for i = 2:M-1
B(i,N) = A(i,j-1) + A(i-1,j) + A(i+1,j)
end
3. In step 3, we want to get the elements of the last row. So the indexing of the first element in the last row should be B(N,1) instead of B(1,N) as written in the first line of the code in step 3.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by