Subscripted assignment dimension mismatch.
Show older comments
i am new in matlab please solve this error
M = imread('C:\Users\DELL\OneDrive\Documents\MRI-images\Folder1\image-1.jpg');
I=rgb2gray(M);
[X, Y, depth]=size(I);
[X,Y]=meshgrid(1:X,1:Y);
output = [I(:), X(:), Y(:)];
n = 50;
x = X;
fx = Y;
% The values f(x_i) are stored in the first column of
% an (n+1)*(n+1) matrix F.
F = zeros(n+1, n+1);
F(:, 1) = fx;
% Compute the Newton divided differences.
for i=1:n
for j=1:i
F(i+1,j+1) = (F(i+1,j)-F(i,j)) / (x(i+1)-x(i-j+1));
end
end
% Print out the coefficients of the interpolation,
% which is given by F(1,1), F(2,2), F(3,3), ... F(n+1,n+1).
% Then the Lagrange interpolation is
% (here all indices have been shifted so it starts from 1 and end at n+1)
% F(1,1) + F(2,2)*(x-x1) + F(3,3)*(x-x1)*(x-x2) + ...
% + F(n+1,n+1)*(x-x1)*(x-x2) ... (x-xn)
% We can use the Matlab command "diag" to get these diagonal elements.
a = diag(F);
% Next, we would like to draw the graph of the Lagrange interpolation.
% To do this, we first need to evaluate the polynomial at a set of points.
% The x and y coordinates of these points are stored in plotx and ploty.
plotx = -2:0.1:3;
% Evaluate the value of the Lagrange interpolation "Horner-style".
ploty = a(n+1)*ones(size(plotx));
for i=n:(-1):1
ploty = a(i) + ploty.*(plotx-x(i));
end
% Draw the graph.
% The curve is the interpolation,
% and the given points (x_i, f(x_i)) are shown as stars.
figure(1);
plot(plotx, ploty, '-', x, fx, '*');
I am getting following error
Subscripted assignment dimension mismatch.
Error in p7 (line 14)
F(:, 1) = fx;
11 Comments
There is no guarantee that your image will have exactly 50 columns, which is what this code assumes.
If you run the line of code below, the execution will stop just before the error is thrown. That will give you a chance to have a loop at your variables and try to find out if your assumptions are valid.
dbstop if error
I have also edited your post. If you use a code section instead of inserting a code example, you will be able to run code in the interface of the forum.
image-pro
on 13 Apr 2022
Rik
on 13 Apr 2022
That depends. Why did you set n to 50? Is that the expected number of columns in the image? Or should you set n based on the number of columns?
Rik
on 13 Apr 2022
You are overwriting the function size with a variable, which is not a good idea, since you're probably using it elsewhere. If you explain in comments to your code what is your intention with the code (and attach the image as a file), we can run your code in the online environment and we might be able to help you.
Since you're getting the same error, the advice is the same: check the variables you have at that point and try to find out which variable is not matching your expectation.
image-pro
on 13 Apr 2022
M = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/963440/image.png');
I = rgb2gray(M);
[X, Y, depth] = size(I);
[X,Y] = meshgrid(1:X,1:Y);
x = X;
y = Y;
sz = length(x);
d = zeros(sz,sz);
xlength = length(x);
DDlim = length(x)-1;
D = zeros(xlength,xlength);
size(D(:,1)),size(y)
D(:,1) = y;
As you can see, y is 512x512, while D(:,1) is only 512x1. That is why it doesn't fit.
You still haven't explained what your code intends to do. You create a swarm of variables, but what do you actually want to achieve by the time you get to the loop?
image-pro
on 14 Apr 2022
Rik
on 14 Apr 2022
I didn't mean on that high level, I mean on the level of implementing the equation you posted in one of your other questions. What is the goal you want to achieve with the code itself up to this point?
image-pro
on 14 Apr 2022
Rik
on 14 Apr 2022
No, it is not. What I mean is something else: why did you write imwread? If you need to find a value, you can use find. The answer is that you don't intend to 'find a value', you want to prepare variables for your loop.
I don't understand your equations well enough to be able to guess how you need to pre-process your image to make it ready for the loop. That is what I'm trying to find out.
The goal of the loop is to calculate the pixel value, the goal of the code prior to the loop is to pre-process your data. Your code is devoid of any comments explaining why you need any of the variables you're creating. That is why we're already 10 comments deep into this conversation without the actual problem being clear yet.
Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!