i am working with huge data (1800000x39 mtrix) in MATLAB. I have to extract a block of matrix from certain interval out of the this large matrix. How can i concatenate the matrix from each iteration.

data=('test.txt'); M=importdata(data);
for i=1:100 m1=M(i:i+3,1:3) end How can i concatenate the matrix generated by each iteration? Thank you in advance for your help.

2 commentaires

Concatenating arrays within loops is a very poor scripting concept in MATLAB. It leads to very slow code due to the repetitive memory allocations required. You can avoid this by preallocating the arrays, or even better learning to write vectorized code. For an explanation of array preallocation:
Thank you for your advice, i will do that..

Réponses (1)

First generate an index of all the rows you need, an then use this index to get the small matrix m:
idx = cell2mat(arrayfun(@(i) ([i:i+3]), 1:100, 'UniformOutput', false));
m = M(idx, 1:3);

Cette question est clôturée.

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by