MATLAB Answers

How do I get my X array to cycle through the code?

1 view (last 30 days)
Kyle Donk
Kyle Donk on 25 Jan 2020
Commented: Walter Roberson on 25 Jan 2020
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

Show 4 older comments
Mohammad Sami
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
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
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.

Sign in to comment.

Accepted Answer

Kyle Donk
Kyle Donk on 25 Jan 2020
%Part 2: Estimating the average number of swaps required to sort
%a random list of numbers
count=0;
for X=[100:100: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
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
end
Y=[average1 average2 average3 average4 average5 average6 average7 average8 average9 average10];
end
plot(X,Y,'o')

  1 Comment

Allen
Allen on 25 Jan 2020
I think you are not understanding how for-loop indexing works. Essentially, they are setup using a variable to identify the index and an array of values that the index will cycle through by increasing the index shifting to the next element in the array. The index does not retain the entire array in memory, it only retains the last value. Thus your initial for-loop defines X as the index and a range of values from 100 to 1000, with increments of 100 (ie - [100,200,300,...,1000]). Each time you step through this loop the value of X is changed to the next value in this array. For example, the first iteration X=100, the second iteration X=200, and the final iteration X=1000.
More importantly, the use of your initial for-loop does not appear to have any effect on the answer, it only makes you find the same solution multiple times. This is why every value of Y appears to be the same.
Replacing your plot command with the following will show you a quick example of how to use a for-loop and that you are actually plotting the same answer multiple time.
axes % Not normally needed, but wanted to use the "hold on" command outside of the for-loop
hold on % A
for i=1:length(Y)
% Plots each element of Y individually by changing the index "i" each iteration
plot(X,Y(i),'o')
legend(sprintf('Iteration: %i',i)) % Creates a label to identify the current iteration
pause(1) % pause execution for 0.5 seconds in order to make the changes perceivable
end
Try setting breakpoints within your code and stepping through your code to see how the values are changing throughout its execution. Below are links to how to use for-loops and how to using debugging tools built into MATLAB.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!