index error,exceeding array dimensions.

Hello Everyone,
I'm here to ask if there is anyone who can help me see where i am doing wrong because i really cannot see what i keep writing that is not accepted by the program
it gives out an error of exceeding index but i see no mistake in the input of the functions and the limits that the index has to run (from 1 until dim i.e)
what is confusing is the fact that it sometimes gives no mistake but lately the same writings have always gotten denied
function [Vet_ord] = ordina(Vet,dim)
for i=1:dim
k=i;
j=i+1;
while j<dim
if Vet(k)>Vet(j)
k=j;
end
j=j+1;
end
if i~=k
temp=Vet(i);
Vet(i)=Vet(k);
Vet(k)=temp;
end
Vet_ord=vet;
end
end
the warning is : index exceeds the number of array elements.
Index must not exceed 1.
Error in ordina (line 6)
if Vet(k)>Vet(j)
thanks everyone in advance for the help.

댓글 수: 5

I think the error lies on your input. From your code, your input works up to
dim = numel(Vet)+1
If dim is greater than the number of itens that you have in your vector, it will arrive to a case where j < dim but you're getting out of your array limits. For instance, running with:
ordina([30,2,4,3,5],7)
You'll have a case where you try to acceed Vet(6) that does not exist.
What are the values of Vet (and the size of Vet) and dim as input to the function?
Also, you can modify the below code
if i~=k
temp=Vet(i);
Vet(i)=Vet(k);
Vet(k)=temp;
end
to a single line -
Vet([i k])=Vet([k i]);
savino cataleta
savino cataleta 2023년 1월 27일
hello,thank you for the tips i appreciate it so much.
The Vet and the its dimension are given by the user in the main page in this form:
N=input('insert the array's dimension') ;
while the array is created by a function
V=createarray(N);
I'll try out your suggestion in order to see if it works that way.
again thanks a lot in advance .
Image Analyst
Image Analyst 2023년 1월 27일
Did you see the solution below, in the official "Answers" section?
Dyuman Joshi
Dyuman Joshi 2023년 1월 27일
@savino cataleta is the value of Dim calculated from Vet or manually inputted?
If the former, why use it as an input when you can obtain the value in the code directly using inbuilt functions.
If the latter, you have to check that Dim and any other variables used in reference to Dim do not exceed the size of Vet.

댓글을 달려면 로그인하십시오.

답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 1월 27일

0 개 추천

Here is the corrected code:
Vet = randi([-9,9], 1, 7);
dim = numel(Vet)+1;
V = ordina(Vet,dim)
V = 1×7
-9 -6 -5 1 5 6 9
function Vet_ord = ordina(Vet,dim)
for i=1:dim-1 % Excess the dimension of Vet
k=i;
j=i+1;
while j<dim-1 % Excess the dimension of Vet
if Vet(k)>Vet(j)
k=j;
end
j=j+1;
end
if i~=k
temp=Vet(i);
Vet(i)=Vet(k);
Vet(k)=temp;
end
Vet_ord=Vet; % The variable name has to be Vet not vet
end
end

댓글 수: 1

savino cataleta
savino cataleta 2023년 1월 27일
hello, i've just tried this out. it keeps telling me it goes over the dimension of the array but it is not true.
i can not understand where it goes wrong.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2023년 1월 27일

댓글:

2023년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by