Subscripted assignment dimension mismatch.

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.
there is another solution to solve the problem
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?
M = imread('C:\Users\DELL\OneDrive\Documents\MRI-images\Folder1\image-1.jpg');
I = rgb2gray(M);
imshow(I);
[X, Y, depth] = size(I);
[X,Y] = meshgrid(1:X,1:Y);
%output = [I(:), X(:), Y(:)];
x = X;
y = Y;
size = length(x);
d = zeros(size,size);
xlength = length(x);
DDlim = length(x)-1;
D = zeros(xlength,xlength);
D(:,1) = y;
for i = 1:DDlim
for j = 1:i
D(i+1,j+1)=(D(i+1,j)-D(i,j))/(x(i+1)-x(i-j+1));
end
end
a = diag(D);
expr = poly2sym(a) ;% This create polynomial from the coefficient matrix
fpe = fplot(expr);
title(fpe.DisplayName)
hold on
figure(1)
plot(x,y,'*')
%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(xlength+1)*ones(size(plotx));
%for i=xlength:(-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, y, '*');
i have change n=50 now again same error is showing .
Subscripted assignment dimension mismatch.
Error in p2 (line 19)
D(:,1) = y;
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.
for this image i have to apply newton divided difference interpolation method to estimate missing value of y.
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)
ans = 1×2
512 1
ans = 1×2
512 512
D(:,1) = y;
Unable to perform assignment because the size of the left side is 512-by-1 and the size of the right side is 512-by-512.
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?
using newton divided difference interpolation method i want to estimate value of effected area. i have taken image and detect disease and after segmentation applied numerical method to evaluate the value of effected area. but i am still not get the correct output. can you send me the code how to achieve this?
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?
The goal of my code is to find the effected pixel value
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.

Sign in to comment.

Answers (0)

Categories

Asked:

on 13 Apr 2022

Commented:

Rik
on 14 Apr 2022

Community Treasure Hunt

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

Start Hunting!