0

I'm new to MATLAB and I'm having problem with a for loop in a cell array. I have a 52x52 cell array B. In every cell, there is a 51x51 matrix. For every cell of the first row of B, I want to calculate the trace and I want the trace-elements in a vector (and smooth them with a spline). The variables dbmus and cs may be overwritten every time, SQED and ddbmus not. I have the following line of code, but I keep getting this error: In an assignment A(I) = B, the number of elements in B and I must be the same.

X = 1:51;                 
xx = linspace(1,51,250);  
SQED = zeros(1,52);
dbmus = zeros(1,52);
ddbmus = zeros(1,52);
for i = 1:52
    SQED(i) = sum(diag(B{1,i}));
    dbmus = transpose(diag(B{1,i}));
    cs = spline(X,[dbmus(1),dbmus,dbmus(end)]);      
    ddbmus(i) = ppval(cs,xx);                        
end

How can I fix this?

chappjc
  • 30,359
  • 6
  • 75
  • 132
Ben
  • 3
  • 1
  • That means that one of the lines within your for loop is attempting to place a vector into an element. What line gives the error? It's either the first or last statement inside the for loop. – jerad Jan 16 '14 at 19:56
  • 1
    use `dbstop if error` and check the sizes/types of your variables. – Daniel Jan 16 '14 at 20:05

1 Answers1

0

ppval evaluates your spline at all points in xx. That produces a vector. You then try to store the vector in ddbmus(i), which is a scalar.

You probably want to store the full vector in rows (or columns) of a matrix. If so:

ddbmus = zeros(52, 250);
for i = 1:52
    % ... existing code
    ddbmus(i, :) = ppval(cs,xx);
end
Peter
  • 14,559
  • 35
  • 55