Need help flipping elements in an array without using built in functions
Ältere Kommentare anzeigen
Here is the problem i am trying to work with:
Develop a user-de
5 Kommentare
per isakson
am 6 Nov. 2014
Your question seems to be truncated.
Yousef
am 6 Nov. 2014
per isakson
am 6 Nov. 2014
Bearbeitet: per isakson
am 6 Nov. 2014
It's your homework(?)   See my answer.
per isakson
am 6 Nov. 2014
Bearbeitet: per isakson
am 9 Nov. 2014
@Image Analyst, Given Walter's rules, isn't it impossible to write a bubble-sort routine? I assume that's the task. Is   +   to be regarded as syntactic sugar for the function   plus
Antworten (1)
per isakson
am 6 Nov. 2014
Bearbeitet: per isakson
am 6 Nov. 2014
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.
 
I'm a big fan of the debugging tools of Matlab! Here are some links on debugging in Matlab
15 Kommentare
Yousef
am 6 Nov. 2014
per isakson
am 6 Nov. 2014
Bearbeitet: per isakson
am 6 Nov. 2014
"just flipped the middle 2 elements"   Yes, and that is what you ask for in the title of your question.
"Develop a user-de"   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?
Yousef
am 6 Nov. 2014
Bearbeitet: per isakson
am 7 Nov. 2014
Yousef
am 6 Nov. 2014
Bearbeitet: per isakson
am 6 Nov. 2014
per isakson
am 6 Nov. 2014
Bearbeitet: per isakson
am 6 Nov. 2014
- 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.
Yousef
am 6 Nov. 2014
per isakson
am 7 Nov. 2014
Bearbeitet: per isakson
am 7 Nov. 2014
- The code you posted in the comment above does not work!
- "using refilling"   I don't understand your description.
Yousef
am 7 Nov. 2014
Bearbeitet: per isakson
am 7 Nov. 2014
per isakson
am 7 Nov. 2014
Bearbeitet: per isakson
am 7 Nov. 2014
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  "using refilling".   Why do you start by picking the 8 and the 9?
It's time you learn how to use the {}Code-button.
 
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
Yousef
am 7 Nov. 2014
per isakson
am 7 Nov. 2014
Bearbeitet: per isakson
am 7 Nov. 2014
"re-flip"   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
Yousef
am 9 Nov. 2014
Bearbeitet: per isakson
am 9 Nov. 2014
per isakson
am 9 Nov. 2014
It's the same code in your last two comments. Please, delete the last one.
per isakson
am 9 Nov. 2014
Bearbeitet: per isakson
am 9 Nov. 2014
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
per isakson
am 10 Nov. 2014
Bearbeitet: per isakson
am 11 Nov. 2014
I run your code. There are obviously problems. Do the following:
- Put breakpoints at the lines after the two last   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
Kategorien
Mehr zu Shifting and Sorting Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!