Preallocation of a matrix: how to shrink it afterwards?

조회 수: 3 (최근 30일)
Samuele Bolotta
Samuele Bolotta 2020년 5월 11일
댓글: Walter Roberson 2020년 5월 11일
I'm trying to create an empty epoch for every epoch number in epochn - epochn is a nx1 matrix. How can I preallocate fullstate efficiently? Thanks!
%Create empty epoch for every epoch number
for t = epochn
idx = find(epochn == t);
fullstate(t) = state(idx);
clear idx
end

답변 (1개)

per isakson
per isakson 2020년 5월 11일
편집: per isakson 2020년 5월 11일
Something like this
fullstate = class_of_state( size(epochn) );
for t = reshape( epochn, 1,[] )
idx = find(epochn == t);
fullstate(t) = state(idx);
clear idx
end
The variable, state, what type of values does it have?
"shrink it afterwards" why would that be necessary?

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by