# Error using reshape Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.

31 views (last 30 days)
Kutlu Yigitturk on 28 Dec 2020
Commented: Image Analyst on 28 Dec 2020
% dening data points, vectors x and y
x_value = [0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5];
y_value = [0 0.2474665 0.8813736 1.550158 2.0947125 2.5320681 2.893444 3.2003349 3.466711 3.7019111 3.9124228];
% dening x in range 0 to 5 with total 100 values
xx = linspace(0,5,100);
plot(x_value, y_value,'O')
%calculation of coeffiecients
for i=1:xx
for j=1:xx
c.c(i,j)=(x_value(1,i)-x_value(1,j));
end
end
c=c.*c;
q=size(c);
c=c';
c(c==0)=[];%deleting the zero values from the matrix
c=reshape(c,q(2)-1,q(1));
c=reshape(c,q(2)-1,q(1)); -> Error in Project (line 19)
c=c';
j=1;
for i=1:xx
d.d(i,j)=prod(c(i,:));
end
d=d.d;
d=d';
for i=1:xx
coff.coff(i,j)=y_value(1,i)./d(1,i);
end
coff=coff.coff;
coff=coff';%coefficients
for i=1:xx
for j=1:xx
p.p(i,j)=(x-x_value(1,j));
end
end
p=p.p;
m=size(p);
p=p';
p(1:xx+1:end)=[];%deleting the diagonal elements
p=reshape(p,m(2)-1,m(1));
p=prod(p);
pol=coff.*p;
pol=sum(pol);%Lagrange polynomial
l=input('Enter a value to compute the b value:');
output=subs(pol,l);
%Displaying output
disp('');
fprintf('Value of input x:=%f\n',double(output));
if l>max(x_value)
x=min(x_value):0.1:l;
y=subs(pol,x);
plot(x,y,'r');
xlabel('X')
ylabel('Y')
else
x=min(x_value):0.1:max(x_value);
y=subs(pol,x);
plot(x,y,'r');
xlabel('X')
ylabel('Y')
end
At line 19, the 'Error using reshape Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension. ' I am getting this error. How can I solve this problem. Thank you for your help.

Image Analyst on 28 Dec 2020
When you say this:
c.c(i,j)=(x_value(1,i)-x_value(1,j));
you're saying that you're assigning the right hand side to a field called "c" that is a member of a structure also (unfortunately) called "c". Why are you doing that?
Then you do this
c=c.*c;
q=size(c);
c=c';
c(c==0)=[];%deleting the zero values from the matrix
c=reshape(c,q(2)-1,q(1));
The first line will not even work since you can't square a structure. But anyway, you get q based on the size of c but then delete some elements from c so the size of c changes. However you never updated q. Why didn't you? And why do you want c to be a 2D array? What is the overall intent of this code? Some kind of filtering, convolution, or gradient?
The whole code is a mess and I think you'll discover the error if you do two things:
1. Use more descriptive variable names, not a collection of names that looks like alphabet soup.
2. Use a comment in front of every chunk of code to describe that that chunk of code actually does.
Image Analyst on 28 Dec 2020
For spline, you can use interp1() or spline(). See attached demo.

R2020b

### Community Treasure Hunt

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

Start Hunting!