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

8 views (last 30 days)
Jose Chow Su
Jose Chow Su on 17 Jun 2019
Edited: Stephen23 on 17 Jun 2019
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)

Accepted Answer

Debasish Samal
Debasish Samal on 17 Jun 2019
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)

More Answers (2)

Stephen23
Stephen23 on 17 Jun 2019
Edited: Stephen23 on 17 Jun 2019
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 on 17 Jun 2019
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.

Community Treasure Hunt

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

Start Hunting!