all submatrices of given order of a given matrix

4 views (last 30 days)
I am new in matlab. Is it possible to compute the all submatrices of given order of a given matrix M? For example suppose we have a matrix M of order m by n, and want to compute the all submatrices of order k by n, for some k <= m.
thanks
  1 Comment
John D'Errico
John D'Errico on 8 Jan 2015
By the way, expect this to be HUGE for even reasonable values for your matrix sizes.
m = 50;
n = 25;
nchoosek(m,n)
ans =
1.2641e+14
So there are roughly 126 trillion such sub-matrices. If you will store them too, it will take a serious amount of memory. Just the work to generate them all will be immense, even if done in a loop rather than all at once.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 8 Jan 2015
Use nchoosek(1:m, k) to find all the possible combination of rows to pick in your matrix, and pick these rows:
%random demo data
m = 5; n = 4;
mx = reshape(1:m*n, m, n);
k = 3;
subrows = num2cell(nchoosek(1:m, k), 2);
subm = cellfun(@(rows) mx(rows, :), subrows, 'UniformOutput', false)
  5 Comments
Daniel Wilson-NUnn
Daniel Wilson-NUnn on 17 Aug 2015
Hi Guillaume, I have been looking at this answer, and I would like to be able to use this code to select all 2x2 matrices of a bigger nxn matrix. Would you be able to advise as to how to do this?
Many thanks
Guillaume
Guillaume on 17 Aug 2015
Daniel, please start a new question. You'll get more people to contribute that way

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!