Extract from a matrix pairs of consecutive values

2 次查看(过去 30 天)
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

回答(1 个)

Florian Rössing
Florian Rössing 2022-11-24
编辑:Florian Rössing 2022-11-24
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by