Extract from a matrix pairs of consecutive values

3 views (last 30 days)
Throughout the following code i can represent random numbers. The problem is that i must be able now, not to plot the array and all the values, rather than that, plot with the scatter functin the pairs of consecutive values, but i dont know how to extract all the pairs. Here is the code i have:
clc;
clear all;
a= 37;
b= 1;
m=64;
f= @(x) mod((a*x)+b, m);
x(1)=1;
for i=1:200
x(i+1)= f(x(i));
end
d=0;
for i=1:200
if x(i+1)==x(1)
d=i;
break;
end
end
d

Answers (1)

Florian Rössing
Florian Rössing on 24 Nov 2022
Edited: Florian Rössing on 24 Nov 2022
I would suggest
clc;
clear all;
a= 37;
b= 1;
m=64;
f= @(x) mod((a*x)+b, m);
x(1)=1;
for i=1:200
x(i+1)= f(x(i));
end
d=0;
for i=1:200
if x(i+1)==x(1)
d=i;
break;
end
end
% if you need the odd vs even
x_odd = x(1:2:end);
x_even = x(2:2:end);
%% if x is of odd length, there are more odd indexed numbers, we need to get rid of one.
if mod(length(x),2)~=0
x_odd=x_odd(1:end-1);
end
scatter(x_odd,x_even);
%if you want all x(i)
scatter(x(1:end-1),x(2:end)) % maybe needs some tweaking when the length of x cant be nicely split

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!