Talha Yousuf
Talha Yousuf

Reputation: 301

How to transform this 2x2 matrix to the 3x3 matrix shown?

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

Answers (3)

Dev-iL
Dev-iL

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

PhoenixBlue
PhoenixBlue

Reputation: 1037

B=zeros(3);
B([1,end],[1,end])=A;

Upvotes: 1

Hunter Jiang
Hunter Jiang

Reputation: 1300

The code following would work:

A=[6 8; 10 16];
B=zeros(3);
B([1,3],[1,3])=A;
B

Upvotes: 1

Related Questions