Reputation: 301
How to convert the matrix below to the one required in Matlab, I tried using logicals but couldn't make logic:
6 8
10 16
required
6 0 8
0 0 0
10 0 16
Upvotes: 0
Views: 2014
Reputation: 24169
The other solutions are probably what you want, and yet, here's a needlessly complicated way to do the same thing:
B = conv2( ones(2), A) .* ~strel('diamond',1).Neighborhood;
The strel
function requires the Image Processing Toolbox, so if you don't have it, you can instead use some variations of the solutions to these questions: 1, 2.
Tested on R2018b.
Upvotes: 1
Reputation: 1300
The code following would work:
A=[6 8; 10 16];
B=zeros(3);
B([1,3],[1,3])=A;
B
Upvotes: 1