ERROR : Index exceeds matrix dimensions.

1 view (last 30 days)
Mouhamad
Mouhamad on 20 Apr 2014
Commented: Geoff Hayes on 11 May 2020
i am new user of matlab, i want to get the number of zero crosses and the total energy of a audio wave, but when i do run this program it gives an error :
>>[ plot(blocks(1,1:len))
Index exceeds matrix dimensions.]
the code :
len = length(blocks);
n = sum(size(blocks)) - len;
min = n+1;
max = 0;
sumthreshtotal = len * sumthresh;
zerocrossthreshtotal = len * zerocrossthresh;
for i = 1:n
currsum = sum(abs(blocks(i,1:len)));
currzerocross = zerocross(blocks(i,1:len));
if or((currsum > sumthreshtotal),(currzerocross > zerocrossthreshtotal))
if i < min
min = i;
end
if i > max;
max = i;
end
end
end
what should i change in order to make it run ?
thank you in advance.
  3 Comments
Huda Zulfiqar
Huda Zulfiqar on 11 May 2020
Index exceeds matrix dimensions.
Error in Green (line 10)
a_g = a( :, :, 2 );
When i run the below code it shows the error "index exceeds matrix dimensions"
can anyone plzz tell me my mistake in this code??
clc;
clear;
close;
% Read the image
a=imread('mouse.png');
% Convert to grayscale incase it is color
a = rgb2gray(a);
a_g = a( :, :, 2 );
b=size(a_g);
a=double(a_g);
% Loop for Getting the Histogram of the image
hist1 = zeros(1,256);
for i=1:b(1)
for j=1:b(2)
for k=0:255
if a(i,j)==k
hist1(k+1)=hist1(k+1)+1;
end
end
end
end
%Generating PDF out of histogram by diving by total no. of pixels
pdf=(1/(b(1)*b(2)))*hist1;
%Generating CDF out of PDF
cdf = zeros(1,256);
cdf(1)=pdf(1);
for i=2:256
cdf(i)=cdf(i-1)+pdf(i);
end
cdf = round(255*cdf);
ep = zeros(b);
for i=1:b(1) %loop tracing the rows of image
for j=1:b(2) %loop tracing thes columns of image
t=(a(i,j)+1); %pixel values in image
ep(i,j)=cdf(t); %Making the ouput image using cdf as the transformation function
end
end
% Loop for Getting the Histogram of the image
hist2 = zeros(1,256);
for i=1:b(1)
for j=1:b(2)
for k=0:255
if ep(i,j)==k
hist2(k+1)=hist2(k+1)+1;
end
end
end
end
subplot(2,2,1);
imshow(uint8(a_g));
title('Original Image');
subplot(2,2,2);
imshow(uint8(ep));
title('Image after Histogram Equalization');
subplot(2,2,3);
stem(hist1);
title('Histogram of the Original Image');
subplot(2,2,4);
stem(hist2);
title('Histogram after Histogram Equalization of the Original Image');
suptitle(' The original green channel image along with image after histogram equalization with their respective histograms')
Geoff Hayes
Geoff Hayes on 11 May 2020
Huda - in the code
% Convert to grayscale incase it is color
a = rgb2gray(a);
a_g = a( :, :, 2 );
when you call rg2gray, a will be a grayscale image with dimensions mxnx1 and so trying to access the green component will fail. You probably want to create a second variable for the grayscale image so that you don't lose the original
% Convert to grayscale incase it is color
aGrayScale = rgb2gray(a);
a_g = a( :, :, 2 );

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!