Preallocation for a cell help needed

1 次查看(过去 30 天)
Hello,
I could not find out how to properly do the preallocation for the following situation. Could you please give me a hand?
Regards
Size of tracks' changes every loop, so I want to do preallocation for it but could not figure out how. (Size of tracks(1,i).nPoints remains same. Only tracks(1,i).matrix changes, 1≤i≤max_n_trks)
%% Interpolation
interp=1;
for param=1:interp
for k=1:max_n_trks % maximum number of tracks
vector = tracks(1,k).matrix;
len = tracks(1,k).nPoints;
for i=len:-1:1
vector(2*i-1,:,:)=vector(i,:,:);
end
tracks(1,k).nPoints = (tracks(1,k).nPoints*2)-1;
len = tracks(1,k).nPoints;
for i=1:2:len-1
vector(i+1,:,:) = (vector(i,:,:) + vector(i+2,:,:))/2;
end
tracks(1,k).matrix = vector;
end
end
note: Now, I found a way:
B = struct('nPoints',repmat({zeros(1)},1,110470),'matrix2',repmat({zeros([n 3])},1,110470))
I can assign the maximum value that n can take, however I want to make it more efficient. How can I do it? For example tracks(1,1).matrix has 3 elements and tracks(1,133).matrix has 170 elements. I do not want to assign 170 to each, I want to separately do to assignment.

采纳的回答

Sean de Wolski
Sean de Wolski 2012-12-10
It sounds like you should assign an empty cell to each value:
S = struct('field1',cell(10,1));
You can then fill in the values as necessary:
for ii = 1:10;
S(ii).field1 = magic(randperm(10,1));
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by