Extract from a matrix pairs of consecutive values

2 Ansichten (letzte 30 Tage)
Álvaro Herrador Fernández
Álvaro Herrador Fernández am 24 Nov. 2022
Bearbeitet: Florian Rössing am 24 Nov. 2022
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 Kommentar
Florian Rössing
Florian Rössing am 24 Nov. 2022
Like you want to plot [x(i),x(i+1)] for all i, or just for odd/even i?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Florian Rössing
Florian Rössing am 24 Nov. 2022
Bearbeitet: Florian Rössing am 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

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by