What is wrong with this code?

9 次查看(过去 30 天)
Avinash Bhatt
Avinash Bhatt 2019-5-2
回答: Vidhi Agarwal about 19 hours 前
I am using mkpp() command to construct the polynomial of 'cameraman.tif' but getting the error-
"Error using mkpp (line 62)
The requested number of polynomial pieces, 3, is incompatible with the proposed size, [1],
of a coefficient and the number, 1, of scalar coefficients provided."
MATLAB code
clc
close all
clear all
% Read an image
X=imread('cameraman.tif');
figure,imshow(X);
[r c noc]=size(X);
% Gray Scale Conveersion
if noc > 1
Xn=rgb2gray(X);
else
Xn=X;
end
% Extracting Histogram from image
figure,imhist(X);
% Extracting pixel intensity values
i=1;
while i ~= 256
for j=1:c
z=X(i,j);
disp(z);
end
i=i+1
end
breaks=[70 80 90 100]
%for b=1:z
% X(b)=X(b)+1;
% disp(X(b));
%end
pp=mkpp(breaks,X(i,j));
Please help me to rectify the error.

回答(1 个)

Vidhi Agarwal
Vidhi Agarwal about 19 hours 前
The error you're encountering with the "mkpp" function is due to a mismatch between the number of polynomial pieces you're trying to create and the dimensions of the coefficients you are providing. The mkpp function is used to create piecewise polynomials, and it requires the number of coefficients to match the number of pieces specified.
To resolve the issue refer to the following key points:
  • You need to provide a coefficient matrix that matches the number of intervals defined by "breaks". If "breaks" has n elements, you will have n-1 polynomial pieces, and thus need n-1 rows of coefficients.
  • You need to define a polynomial representation for the image data.
Below is the sample code for defining "breaks" and polynomial representation for "mkpp" function:
% Define breakpoints for 3 intervals
breaks = [70, 80, 90, 100];
% Define coefficients for each polynomial piece
% Let's assume we want linear polynomials for simplicity
% Each row represents a polynomial: ax + b
% For simplicity, let's use random coefficients
coeffs = [1, 2; % Polynomial for interval [70, 80]
3, 4; % Polynomial for interval [80, 90]
5, 6]; % Polynomial for interval [90, 100]
% Create the piecewise polynomial
pp = mkpp(breaks, coeffs);
% Evaluate the piecewise polynomial at some points
x = linspace(70, 100, 100);
y = ppval(pp, x);
% Plot the piecewise polynomial
figure;
plot(x, y);
title('Piecewise Polynomial');
xlabel('x');
ylabel('y');
For better understanding of "mkpp" function and "breaks" in mkpp refer to the following documentation.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Polynomials 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by