Asked by Alessandro Marutti
on 22 May 2019

Hi guys, I know there're some similar questions around the comunity but I didn't find an answer to understand my problem.

I'm writing a code to sort an vector without using "sort" function, however matlab returns me an error that tells :"Index exceeds the number of array elements (6); min = q(s);". I put "min" equal to the element of vector "q" with index "s" so I don't understand why matlab tells me this error.

I hope you can help me, thanks.

q = [3 1 -1 4 2 0 ]

qRiserva = 0; % variable to temporarily keep the element of q(s)

s = 1; % index for outer 'for cycle'

min = q(s);

max = q(1);

i = 1; % index for deeper 'for cycle'

for s = s :length(q)

for i = s: length(q)

if q(i) <= min

qRiserva(s) = q(s)

min = q(i)

q(s) = min

q(i) = qRiserva(s)

end

end

s = s+1;

min = q(s);

end

Opportunities for recent engineering grads.

Apply Today
## 7 Comments

## Star Strider (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707721

## Alessandro Marutti (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707726

## Star Strider (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707734

## Adam (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707738

## Alessandro Marutti (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707746

## Star Strider (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707749

## Alessandro Marutti (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/463506-index-exceeds-the-number-of-array-elements-6#comment_707753

Sign in to comment.