How to pre-allocate a matrix (from cell) when dimensions change
3 次查看(过去 30 天)
显示 更早的评论
Dear all,
B is a 1x3 cell. The 1st element is empty (it doesn't matter because my loop will start at position 2). the other elements are 218x2 and 266x2.
I have a loop that is meant to do 'stuff', resulting in a vector 'cofcoefvector' that is 2X13. What I'm doing is partly correct, partly because row 1 and row 2 have the same values (the values are correct for r=3). Somehow, I repeat the operation. I think that's because I over-write C (see below) in the loop, and that because I haven't indexed it. I haven't done so, because I'm not able to.
for r = 2: size(B,2);
C=cell2mat(B(r));
for m=1:size(A,2);
j=A(m);
tempmat1 = corrcoef(bpassf(C(:,2),6,32),lagmatrix(bpassf(C(:,1),6,32),j),'rows','complete');
for row=1:size(corcoefvector,1);
corcoefvector(row,m) = tempmat1(1,2); %this puts the same results in both rows. why?
end
end %this is the end of for t = etc, but it just creates 2 identical copies
end
I thought that was because I (think, going through questions asked before on related issues) over-write C (see above) in the loop, and that because I haven't indexed it. I haven't done so, because I'm not able to.
So I took the largest dimension of the content of B, that is, 266x2 and pre-allocate C before the loop, so
C = zeros(266,2);
and change C=cell2mat(B(r)); to C(r)=cell2mat(B(r));
To no avail, I get 'Unable to perform assignment because the left and right sides have a different number of elements.'
Any thougths?
0 个评论
回答(1 个)
Jan
2021-7-24
Simplify
C=cell2mat(B(r));
to
C = B{r};
This creates a cheap shared data copy, so C does not occupy additional memory: it re-uses the data of B{r}. Therefore a pre-allocation is not useful here.
I do not see a reason to collect the diffent values of C by
C{r} = B{r};
because then C and B contain identical elements.
3 个评论
Jan
2021-7-26
编辑:Jan
2021-7-26
Simplifying cell2mat(B(r)) to B{r} does not solve any problem, but the code gets nicer and faster. This was a general hint only.
Pre-allocating C before the loop is not useful. Most of all:
C = zeros(266,2);
C(r) = cell2mat(B(r));
% Or with my nicer and cheaper code suggestion:
C(r) = B{r};
cannot work, because C(r) is a scalar, but B{r} contains a matrix.
Your explanations still did not clarify, why
C = B{r};
does not satisfy your needs.
You've asked:
for row=1:size(corcoefvector,1);
corcoefvector(row,m) = tempmat1(1,2); %this puts the same results in both rows. why?
end
tempmat1(1,2) is a scalar value. Of course you write it to all elements of corcoefvector(row, m).
By the way, this loop can be avoided also. Faster and easier:
corcoefvector(:, m) = tempmat1(1,2);
Summary: A simplified version of the complete code:
for r = 2:numel(B);
C = B{r};
for m = 1:size(A,2);
j = A(m);
temp = corrcoef(bpassf(C(:,2), 6, 32), ...
lagmatrix(bpassf(C(:,1), 6, 32), j), 'rows', 'complete');
corcoefvector(:, m) = temp(1, 2);
end
end
What do you want to change now?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!