"index exceeds matrix dimensions" error.

1 visualización (últimos 30 días)
Daniel
Daniel el 8 de Mzo. de 2011
Comentada: Walter Roberson el 7 de Mzo. de 2017
I keep getting the "index exceeds matrix dimensions" error when I get to problem 3 with my code. If i were to run the code separately and using clc and clear, it works fine. I also have to use clc and clear every time i want to run my code. Is there any way I can run it without doing so? Thank you! This is my code:
%Problem 1
disp('Answers to Problem 1: ')
fprintf('\n')
N = input('Please enter an integer: ');
while (N < 0 ||(N~=fix(N)));
disp('You have entered an invalid integer. Please try again. ')
N = input('Please enter an integer: ');
end
a = sum(1:N);
fprintf('The sum of integers from 1 to %.0f is: %.2f\n ',N,a)
sum = 0;
for a = 1:N;
sum = sum+a;
end
fprintf('The sum of integers 1 to %.0f is: %.2f\n ',N,sum);
%Problem 2
fprintf('\n')
disp('Answers to Problem 2: ')
fprintf('\n')
K = input('Please enter an integer: ');
product = 1;
while (K < 0 ||(K~=fix(K)))
disp('You have entered an invalid number. Please try again. ')
K = input('Please enter an integer: ');
end
y = prod(1:K);
fprintf('The product of integers 1 to %.0f is: %.1f\n ',K,y)
for y = [1:K];
product = product*y;
end
fprintf('The product of integers 1 to %.0f is: %.1f\n',K,product)
%Problem 3
fprintf('\n')
disp('Answers to Problem 3: ')
fprintf('\n')
Vector = input('Enter a vector while using [] around the numbers: ');
b = sum(Vector);
fprintf('The sum of the elements in the vector = %.3f\n ',b)
sumvec = 0;
w = length(Vector);
for e = 1:w
sumvec = sumvec + Vector(e);
end
fprintf('The sum of the elements in the vector = %.3f\n ',sumvec)
%Problem 4
fprintf('\n')
disp('Answers to Problems 4: ')
fprintf('\n')
P = input('Please enter a value for the first number: ');
Q = input('Please enter a value for the second number: ');
if (P < 0||(P~=fix(P)||(Q~=fix(Q)||(P > Q))))
disp('Please enter valid integers.')
fprintf('\n')
disp('I.E. Whole numbers & first number must be less than the second number.')
P = input('Please enter a value for the first number: ');
Q = input('Please enter a value for the second number: ');
else
t = prod(P:Q);
fprintf('The product of %d to %d is: %d\n ',P,Q,t)
product = 1;
for u = [P:Q];
product = product*u;
end
fprintf('The product of %d to %d is: %d\n ',P,Q,product)
end
%Problem 5
fprintf('\n')
disp('Answer to Problem 5: ')
fprintf('\n')
for k = 1:4
for t = 4:-1:k
fprintf('*')
end
fprintf('\n')
end
for k = 4:-1:1
for t = 4:-1:k
fprintf('^')
end
fprintf('\n')
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Mzo. de 2011
In problem 1, you defined "sum" as a variable. Then in problem 3, you attempt to use "sum" as a function.

Más respuestas (4)

Sean de Wolski
Sean de Wolski el 8 de Mzo. de 2011
It probably means that whatever variable you're using to index is already defined elsewhere and not redefined in your code. Type
dbstop if error
and inspect the variables when it errors out. You don't need clc and you should be able to use
clearvars
instead of clear; just insert it at the top of the script.

Daniel
Daniel el 8 de Mzo. de 2011
Ah, thank you walter! i changed the sum variable to a different name and it worked. and thank you sean! i could also just write "clear" before each code in each problem and that would also take care of my problem.

maged al-dhaeebi
maged al-dhaeebi el 22 de Abr. de 2013
Editada: Walter Roberson el 22 de Abr. de 2013
Index exceeds matrix dimensions.
I have the same probelm in my code hwo to solve this
my code it :
FolderNames={'Data0', 'Data45','Data90','Data135','Data180','Data225','Data270','Data315'}
FolderIndx=1
SaveFileName=strcat('ProcessData',num2str(FolderIndx));
SaveImageFileName=strcat('ImageData',num2str(FolderIndx));
AngleAr=45*[4,5,6,7,0,1,2,3]';
FoldName=FolderNames{FolderIndx}
list=ls(FoldName)
for FrqIndx=1 : 3
FileName=strcat(FoldName,'\', list(FrqIndx+2,:));
StrData=load(FileName);
[XData,YData,ZData,ImageData]=ProcessRawData(StrData,AngleAr,FolderIndx)
SpotsDataX{FrqIndx}=XData;
SpotsDataY{FrqIndx}=YData;
SpotsDataZ{FrqIndx}=ZData;
Images{FrqIndx}=ImageData;
size(Images)
end
save (SaveFileName,'SpotsDataX','SpotsDataY','SpotsDataZ');
save (SaveImageFileName,'Images');
  1 comentario
Walter Roberson
Walter Roberson el 22 de Abr. de 2013
Which line does the problem occur on?
Your code assumes that each of those folders already contains at least 5 directory entries including "." and ".." (which are not promised to be the first two entries, by the way.) If one of the folders contains fewer entries then you would get index out of range accessing list(FrqIndx+2,:)

Iniciar sesión para comentar.


Manjiree Waikar
Manjiree Waikar el 7 de Mzo. de 2017
Editada: Walter Roberson el 7 de Mzo. de 2017
I have this error in my code as
Index exceeds matrix dimensions. Error in clusteredit (line 21) green = imIDX(:,:,2); % Green channel
Please help me how to tackle this error.
My code is-
clear all;
newImageRGB = imread('p2_left_norm_1.bmp');
figure;
imshow(newImageRGB);
title('Original image');
grayImage = rgb2gray(newImageRGB);
figure;
imshow(grayImage);
title('grayscale image');
afterWiener = wiener2(grayImage,[6 6]);
figure;
imshow(afterWiener);
title('Wiener filter');
imData=reshape(afterWiener,[],1);
imData=im2double(imData);
[IDX mn]=kmeans(imData,3);
imIDX=reshape(IDX,size(afterWiener));
red = imIDX(:,:,1); % Red channel
green = imIDX(:,:,2); % Green channel
blue = imIDX(:,:,3); % Blue channel
a = zeros(size(imIDX, 1), size(imIDX, 2));
just_red = cat(3, red, a, a);
just_green = cat(3, a, green, a);
just_blue = cat(3, a, a, blue);
back_to_original_img = cat(3, red, green, blue);
figure,
imshow(just_red),
title('Red channel')
figure,
imshow(just_green),
title('Green channel')
figure,
imshow(just_blue),
title('Blue channel')
  1 comentario
Walter Roberson
Walter Roberson el 7 de Mzo. de 2017
Looking in reverse order, you have
green = imIDX(:,:,2); % Green channel
after
imIDX=reshape(IDX,size(afterWiener));
after
afterWiener = wiener2(grayImage,[6 6]);
after
grayImage = rgb2gray(newImageRGB);
So you are taking an RGB image, transforming it to gray, doing a wiener2 transform on it, getting a 2D result. The size of the 2D result is used to reshape the matrix IDX, so the result of the reshape is going to be 2D. You then try to index the third dimension of that 2D matrix.
Possibly at some point after reshaping IDX, you wanted to use those indices to index into the original color image, such as
R = newImageRGB(:,:,1);
G = newImageRGB(:,:,2);
B = newImageRGB(:,:,3);
red = R(IDX);
green = G(IDX);
blue = B(IDX);

Iniciar sesión para comentar.

Categorías

Más información sobre Signal Processing Toolbox en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by