Need help flipping elements in an array without using built in functions

Here is the problem i am trying to work with:
Develop a user-de

5 Comments

basically i want to refill elements in this array (1 4 7 3 2 9 8 6) and sort them in order without using the built in functions
It's your homework(?) &nbsp See my answer.
@Image Analyst, Given Walter's rules, isn't it impossible to write a bubble-sort routine? I assume that's the task. Is &nbsp + &nbsp to be regarded as syntactic sugar for the function &nbsp plus

Sign in to comment.

Answers (1)

Hint:
vec = 1:12;
ix = 6;
vec = vec( [(1:ix-2),ix,ix-1,(ix+1:end)] )
vec =
1 2 3 4 6 5 7 8 9 10 11 12
The fifth and sixth elements are flipped.
&nbsp
I'm a big fan of the debugging tools of Matlab! Here are some links on debugging in Matlab

15 Comments

I couldn't get your code to work for me. what i want is to arrange all the elements in order, your code just flipped the middle 2 elements
"just flipped the middle 2 elements" &nbsp Yes, and that is what you ask for in the title of your question.
"Develop a user-de" &nbsp doesn't make sense to me.
I guess, based on the tags, that you have an assignment to implement a bubble sort routine. Thus, what have you done so far and which specific problems do you have?
sorry i posted the entire question but for some reason it was deleted and only the first sentence is shown.
  1. let say i have these elements in an array: [3 8 7 1 9 5 7 2],
  2. i want to sort them in order from left to write.
  3. i am not allowed to use built in functions.
  4. what i want is similar to the bubble method but it has to be done using refilling.
  5. like in the above array, the [8,7,1] will flip to bring the 8 before 9,
  6. and then the same for the 7 and so on until all elements are sorted: [1 2 3 4 5 6 7 8 9].
I hope this is good explanation.
here is the bubble method i have done:
x=o;
for j=1:numel(randomArray)-1
for i=1:numel(randomArray)-1
if (randomArray(i)= randomArray(i+1);
x = randomArray(i);
randomArray(i)= randomArray(i+1);
randomArray(i+1)=x;
end
end
end
  • Have you tried to run your code?
  • The code contains some typing mistakes. Any error messages?
  • Did you use Matlabs debugging tools? I added a couple of links to my answer.
With the help of a little debugging your code will do the job.
I have ran this code and it works fine except I can't use it because it is the bubble sort. I need to flip them like i mentioned in the above comment
  • The code you posted in the comment above does not work!
  • "using refilling" &nbsp I don't understand your description.
ok i had some mistakes in the code but i ran it again now and it worked try this:
function bubbleSortFlip= reflipSort(randomArray)
x=0;
for j=1:numel(randomArray)-1
for i=1:numel(randomArray)-1
if (randomArray(i)> randomArray(i+1));
x = randomArray(i);
randomArray(i)= randomArray(i+1);
randomArray(i+1)=x;
end
end
end
tic
output=(randomArray)
toc
end
just create an array called randomArray with variables from say 1:9
Yes, this is bubblesort and replacing the outer for-loop by a while-loop might save a few cpu-cycles. And you can replace the three lines in the if-end statement by
randomArray = randomArray( [(1:ii-1),ii+1,ii,(ii+2:end)] );
However, I still don't understand your description of&nbsp "using refilling". &nbsp Why do you start by picking the 8 and the 9?
It's time you learn how to use the {}Code-button.
&nbsp
The purpose of the exercise might be to flip a vector this way:
>> vec = 1:12;
>> vec(end:-1:1)
ans =
12 11 10 9 8 7 6 5 4 3 2 1
i meant using re-flip method. keep flipping until you line them up. i have a pdf that explains the assignment but it won't let me post it here.
"re-flip" &nbsp doesn't help me.
In your description above you select a sub-sequence, [8,7,1], which neither includes the first element nor the last of the full sequence. I guess the sub-sequence is "left" to the maximum value and starts with the second largest value. However, that doesn't make sense because it say little about about how to proceed.
Over and out
ok so far i've gotten to the code below. what i need now is to pull the last element aside and make the code run again to bring the next smallest element right before the last one.
function mat(a)
maximum=0;
y=0;
flip=0;
reflip=0;
tic
for k=1:numel(a)
for i=1:numel(a)
if maximum < a(i)
maximum = a(i);
max_index=i;
end
end
flip = [a(max_index:-1:1), a(max_index+1:end)];
reflip = [flip(end-k+1:-1:1), flip(end-k+2:end)];
a = reflip;
It's the same code in your last two comments. Please, delete the last one.
Try the following steps
  • describe exactly what the algorithm shall do. ... given input ..., produce output. Ascending or descending?
  • describe in words how you envision that the algorithm shall work
  • use pseudo code to describe the algorithm
  • return to your Malab code
  • step through a couple of really simple examples
I run your code. There are obviously problems. Do the following:
  • Put breakpoints at the lines after the two last &nbsp disp( * )
  • Step through the code with the double green arrow,[Continue].
>> list = randi( 12, [1,6] );
>> list = reflip( list )
9 10 4 9 8 2
10 9 4 9 8 2
18 reflip = [flip(end-k+1:-1:1), flip(end-k+2:end)];
2 8 9 4 9 10
8 2 9 4 9 10
9 4 9 2 8 10
4 9 9 2 8 10
2 9 9 4 8 10
9 2 9 4 8 10
9 2 9 4 8 10
2 9 9 4 8 10
9 2 9 4 8 10
2 9 9 4 8 10
2 9 9 4 8 10
list =
2 9 9 4 8 10
>>
where
function vec = reflip( vec )
%{
list = randi( 12, [1,6] );
list = reflip( list )
%}
maximum=0;
disp( vec )
for k=1:numel(vec)
for ii=1:numel(vec)
if maximum < vec(ii)
maximum = vec(ii);
max_index=ii;
end
end
flip = [vec(max_index:-1:1), vec(max_index+1:end)];
disp( flip )
reflip = [flip(end-k+1:-1:1), flip(end-k+2:end)];
disp( reflip )
vec = reflip;
end
end

Sign in to comment.

Categories

Asked:

on 6 Nov 2014

Edited:

on 11 Nov 2014

Community Treasure Hunt

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

Start Hunting!