How do I get my X array to cycle through the code?
Show older comments
I have an array X=[100 200 300 400 500 600 700 800 900 1000]. But throughout my code the X value only equals 1000. I need to have the code use each X value in the array, but I'm not sure why it isn't cycling through X one at a time. Can anyone help with this?
7 Comments
Stephan
on 25 Jan 2020
we need to see your code
Stephan
on 25 Jan 2020
Kyle Donk's "answer" moved here:
%Part 2: Estimating the average number of swaps required to sort
%a random list of numbers
count=0;
for X=[100; 200; 300; 400; 500; 600; 700; 800; 900; 1000]
num_of_trials=100;
for num_of_trials=1:100
N=100;
for i=1:N-1
for x=randi([1,999],100,1)
if x(i)>x(i+1)
temp=x(i);
x(i)=x(i+1);
x(i+1)=temp;
count=count+1
end
end
end
average1=count/100;
average2=count/100;
average3=count/100;
average4=count/100;
average5=count/100;
average6=count/100;
average7=count/100;
average8=count/100;
average9=count/100;
average10=count/100;
end
end
Y=[average1 average2 average3 average4 average5 average6 average7 average8 average9 average10];
plot(X,Y,'o')
I need to find a way to make average1 equal the average number of swaps when X equals 100 and so forth and so on...
Because this is a school assignment, please do not give me the full complete answer, I just need guidance! Thank you so much!
Mohammad Sami
on 25 Jan 2020
the issue is with the assignment of X in the for loop.
It needs to be a row vector.
change it to X = [100:100:1000];
Kyle Donk
on 25 Jan 2020
Mohammad Sami
on 25 Jan 2020
you are calculating the averages outside the loop. they all use the same count variable. hence they are all the same.
also your code will not sort the data which you are trying to do.
Allen
on 25 Jan 2020
Perhaps you can provide an sample portion of your code that both assigns and calls the variable X. This will help us to understand what the underlying issue is.
Walter Roberson
on 25 Jan 2020
for x=randi([1,999],1,100)
When you do that, x will be set to each scalar from the vector of length 100.
if x(i)>x(i+1)
because x will be a scalar, x(i) will not exist for i > 1
The way you have it at the moment with
for x=randi([1,999],100,1)
makes x equal to the entire random vector at one go, and x(i) then is defined. However, you are also randomizing the entire x vector in each iteration of the for i loop, and that is not going to get you any sensical answers.
You need a structure that is more like
for some many trials
x = random vector
for i = 1 : length(x)
do your swapping if appropriate
end
end
However, I have to wonder what the original question about swapping is. It looks to me like a question about the number of swaps that would be needed for a Bubble Sort. If so then you have the problem that your code does not implement a Bubble Sort: it only implements a single pass of a Bubble Sort but Bubble Sort requires multiple passes.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!