How can i divide a matrix into submatrices of given dimensions in automatic way?
example:
data=reshape(1:88,11,8); %create a matrix
[mpixel,npixel]=size(data); %matrix's dimensions
m=2; %rows of the submatrix
n=3; %columns of the submatrix
v_w=floor(mpixel/m); %number of "vertical" submatrices
h_w=floor(npixel/n); %number of "horizontal" submatrices
cropdata=data(1:v_w*m,1:h_w*n);%crop matrix that can be diveded using that mxn submatrices
%
newdata=mat2cell(cropdata,[m m m m m],[n n]);
This code work but if I have bigger matrix I must continue to explicit [m m m...],[n n n...] in the last command and it's crazy. How can i use the command mat2cell with a smarter way?

댓글 수: 4

Adam
Adam 2017년 12월 12일
Why do you want to break your data up rather than just indexing into it and leaving it in one neat array?
Francesco Boari
Francesco Boari 2017년 12월 12일
편집: Francesco Boari 2017년 12월 12일
Cause I need to have all the small matrices for other processing, in authomatic way. I can't call each time a part of the original matrix. The problem is a little bit more complicated then that in the example.
Image Analyst
Image Analyst 2017년 12월 12일
Why not just use blockproc() and let that handle the getting of the submatrices?
Francesco Boari
Francesco Boari 2017년 12월 13일
I see the documentation of blockproc(), it processes block by block but at the end the output it's the input image/matrix.

댓글을 달려면 로그인하십시오.

 채택된 답변

Guillaume
Guillaume 2017년 12월 13일

0 개 추천

As others have said, there may be no need to explicitly split the matrix, but if you must what is wrong with:
newdata = mat2cell(cropdata, repelem(m, v_w), repelem(n, h_w));

댓글 수: 1

Francesco Boari
Francesco Boari 2017년 12월 13일
Thanks! It works. Meanwhile i have found a different way leaving, as someone said, the original matrix and indexing.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2017년 12월 12일

댓글:

2017년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by