Extract from a matrix pairs of consecutive values

5 visualizaciones (últimos 30 días)
Álvaro Herrador Fernández
Álvaro Herrador Fernández el 24 de Nov. de 2022
Editada: Florian Rössing el 24 de Nov. de 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 comentario
Florian Rössing
Florian Rössing el 24 de Nov. de 2022
Like you want to plot [x(i),x(i+1)] for all i, or just for odd/even i?

Iniciar sesión para comentar.

Respuestas (1)

Florian Rössing
Florian Rössing el 24 de Nov. de 2022
Editada: Florian Rössing el 24 de Nov. de 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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by