MATLAB: CVX: In an assignment A(:) = B, the number of elements in A and B must be the same.

beginnercell arraycvx

Hi all,
I am very new to Matlab and CVX. I have only been using them for the last few months and my background is in engineering not mathematics. I am having the following problem. I am trying to minimise the following objective function:
sum(sum_square(p1(:,2:30)-p1(:,1:29)))
Based on the requirements of the following equations:
for i = 1:29
V = 900;
CL(i) = (W(i).*g)/(0.5*density*(V^2)*S);
CD(i) = CDo + ((CL(i).^2)/(3.14*AR*e));
T = ((CD(i)/CL(i))*(W(i)*g))/1000; %convert to KN
cp(i) = {p1(1,i), p1(2,i); p1(1,i+1), p1(2,i+1)};
disp(i) = pdist(cp(i),'euclidean')*1000; %convert to m
Wf(i) = SFC*T*(disp(i)/V);
W(i+1) = W(i)-Wf(i);
end
Given variables: W, g, V, S, CDo, SFC, density
When I run this I get the following error:
Error using cvx/subsasgn (line 39)
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in fueloptimal (line 79) cp(i) = {p1(1,i), p1(2,i); p1(1,i+1), p1(2,i+1)};
Can anybody help me with this problem?
Thank you in advance.

Best Answer

In this line
cp(i) = {p1(1,i), p1(2,i); p1(1,i+1), p1(2,i+1)};
you're setting cp(i) to a cell. That's what the braces mean - it's a cell. Inside the cell you can have an array, a string, a structure, another cell, just about anything. See the FAQ for a good intuitive explanation: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
But you can't have p1(1,i), p1(2,i); p1(1,i+1), p1(2,i+1) inside a cell because that's not an array. Those values need to be enclosed by brackets to turn them into an array. And once they're an array, THEN you can stick them into a cell and make that the i'th cell of the cell array called "cp". Understand? You will if you read the FAQ.
I'll do it in two steps just to be explicit:
thisArray = [p1(1,i), p1(2,i); p1(1,i+1), p1(2,i+1)];
cp(i) = {thisArray}; % Equivalent to cp{i} = thisArray;