Extracting the sub-matrix

335 Ansichten (letzte 30 Tage)
cikalekli
cikalekli am 27 Okt. 2021
Bearbeitet: cikalekli am 28 Okt. 2021
Hello, while I'm practising on Matlab about extracting matrix and here there was a thing which I stucked while doing it. (Plus, I also added my code which I have tried to solve but it was not right way to solve it.)
First I've let A = [1 2 3; 4 5 6; 7 8 9] be a matrix of the size 3-by-3. After that I wanted to extract that sub-matrix such as B = [2 3; 5 6] of the size 2-by-2 or something else.
Finally I just wanted to create the following matrix by typing two commands like that:
E =
0 0 0 0 0
0 0 0 0 0
0 0 1 2 3
0 0 4 5 6
0 0 7 8 9
However I could not really understand to pull of this B = [2 3; 5 6] sub matrix from the A matrix to find out matrix E.
I just want to understand its logic please.
Sincerely...

Akzeptierte Antwort

David Hill
David Hill am 27 Okt. 2021
A = [1 2 3; 4 5 6; 7 8 9];
B=A(1:2,2:3);
E=blkdiag(zeros(2),A);
  1 Kommentar
cikalekli
cikalekli am 28 Okt. 2021
Bearbeitet: cikalekli am 28 Okt. 2021
I'm really, really grateful for that. Now I will understand better by giving different values myself with this code structure. Thank you again, I wish you a wondrous day.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

James Tursa
James Tursa am 27 Okt. 2021
Bearbeitet: James Tursa am 27 Okt. 2021
You might benefit from going through the onramp tutorials found here:
Sub-matrices use colon indexing. E.g.,
M(3:5,7:9) is the sub-matrix of M in rows 3 thru 5 and columns 7 thru 9.
This syntax can be used for extracting as well as assigning. E.g.,
M = whatever; % some arbitrary matrix
N = M(3:5,7:9); % extract a sub-matrix of M and store it in a variable called N
M(2:4,5:8) = whatever; % assign something to a sub-matrix of M
Also see the following:
  1 Kommentar
cikalekli
cikalekli am 28 Okt. 2021
Ah really thank you for the example and also the site links. As you describe, I will now try to read these links you gave and try to understand them better. Seriously thank you so much for your support.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by