Matlab: how to extract blocks from a large matrix

In summary, to extract 4 blocks out of a 24000x24000 sparse matrix and create a block diagonal matrix using the MATLAB command 'blkdiag', the following code can be used:[m,n] = size(A);A1 = A(1:6000,1:6000);A2 = A(1:6000,6001:n);A3 = A(6001:m,1:6000);A4 = A(6001:m,6001:n);AA = blkdiag(A1,A2,A3,A4);
  • #1
confi999
19
0
Hi,
I have a 24000x24000 sparse matrix. I want to extract 4 blocks out of it;
(0,0) to (6000,6000)
(6001,6001) to (12000,12000)
(12001,12001) to (18000,18000)
(18001,18001) to (24000,24000)

By use of those my aim is to make a block diagonal matrix using MATLAB command 'blkdiag' .

Can anyone please help me with the MATLAB code to extract those 4 blocks.
Thank you so much.
 
Physics news on Phys.org
  • #2
confi999 said:
Hi,
I have a 24000x24000 sparse matrix. I want to extract 4 blocks out of it;
(0,0) to (6000,6000)
(6001,6001) to (12000,12000)
(12001,12001) to (18000,18000)
(18001,18001) to (24000,24000)

By use of those my aim is to make a block diagonal matrix using MATLAB command 'blkdiag' .

Can anyone please help me with the MATLAB code to extract those 4 blocks.
Thank you so much.

I haven't done it before but my guess, would be is you create an empty scarce matrix, and then equate ellements using the colin operator.
 
  • #3
You can use the semicolon to select, let your matrix be A, then the blocks you want to get is A1,A2,A3,A4
Code:
[m,n] = size(A);
A1 = A(1:6000,1:6000);
A2 = A(1:6000,6001:n);
A3 = A(6001:m,1:6000);
A4 = A(6001:m,6001:n);
AA = blkdiag(A1,A2,A3,A4);
 
  • #4
confi999 said:
Hi,
I have a 24000x24000 sparse matrix. I want to extract 4 blocks out of it;
(0,0) to (6000,6000)
(6001,6001) to (12000,12000)
(12001,12001) to (18000,18000)
(18001,18001) to (24000,24000)

By use of those my aim is to make a block diagonal matrix using MATLAB command 'blkdiag' .

Can anyone please help me with the MATLAB code to extract those 4 blocks.
Thank you so much.

If I am understanding you correctly, you want to extract a square matrix
out of a larger matrix. if you want a section out of an array, you just call the array using those indecies. so uf your matrix is A:

(0,0) to (6000,6000) would be

A(1:6001,1:6001)

Remember in Matlab array/matrix indexes start at 1 not 0.

(6001,6001) to (12000,12000) would be

A(6002:12001,6002:12001)

and so on.
 

What is the purpose of extracting blocks from a large matrix in Matlab?

The purpose of extracting blocks from a large matrix in Matlab is to select and extract a specific portion of data from the matrix for further analysis or manipulation. This can be useful when dealing with large datasets where only certain portions of the data are relevant to the analysis.

How can I extract a block from a large matrix in Matlab?

To extract a block from a large matrix in Matlab, you can use the built-in function blockproc. This function allows you to specify the size and location of the block you want to extract, and also provides options for processing the extracted block.

Can I extract non-contiguous blocks from a large matrix in Matlab?

Yes, it is possible to extract non-contiguous blocks from a large matrix in Matlab using the blockproc function. You can specify the blocks you want to extract by providing a list of coordinates or by using a logical mask.

Is it possible to extract blocks from a large matrix without using the blockproc function?

Yes, it is possible to extract blocks from a large matrix without using the blockproc function. You can use indexing to specify the rows and columns of the matrix you want to extract, and then use the reshape function to convert the extracted data into a matrix.

Can I extract blocks from a large matrix and save them as a new matrix?

Yes, you can extract blocks from a large matrix in Matlab and save them as a new matrix. You can use the blockproc function to extract the blocks and then assign the extracted data to a new variable. Alternatively, you can use indexing and the reshape function to extract and save the blocks as a new matrix.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
7K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
9K
Back
Top