Borrar filtros
Borrar filtros

Parameter sweep with meshgrid using a function containing a for loop

10 visualizaciones (últimos 30 días)
I am calculating the time evolution of the following system using a for loop as in the code below.
trainLen = 30;
initLen = 3;
data = [1:1:trainLen]; %%Some data
inSize = 1;
resSize = 50;
Win = (rand(resSize,1+inSize)-0.5) .* 1;
W = rand(resSize,resSize)-0.5;
a = 0.95;
X = zeros(1+inSize+resSize,trainLen-initLen);
x = rand(resSize,trainLen);
for t = 1:trainLen
u = data(t);
x(:,t+1) = (1-a)*x(:,t) + a*tanh( Win*[1;u] + W*x(:,t) );
if t > initLen
X(:,t-initLen) = [1;u;x(:,t)];
end
end
I would like to perform a parameter sweep over the variables a (a scalar) and W (a matrix) using meshgrid to try and avoid using two for loops, but I am struggling rewriting the (time evolution) for loop as an anonymous function to be used with a and W, as in this example.
Any help would be appreciated.
  2 comentarios
KSSV
KSSV el 28 de Jun. de 2023
You mean to say you want to change the code to the case where variables a, w are matrices? Show us the code which is not working for you.
Valentina Baccetti
Valentina Baccetti el 29 de Jun. de 2023
Thanks KSSV, apologies I didn't make myself clear. In my original code a and W are, respectively, a scalar and a matrix. What I have done so far is performing the parameter sweep using two for loops
trainLen = 30;
initLen = 3;
data = [1:1:trainLen]; %%Some data
inSize = 1;
resSize = 50;
Win = (rand(resSize,1+inSize)-0.5) .* 1;
W = rand(resSize,resSize)-0.5;
a = [0.2,0.5,0.7,0.95]; %Now a vector
Wmult = [0.13, 0.144, 0.18, 0.2];
X = zeros(1+inSize+resSize,trainLen-initLen, size(a,2),size(Wmult,2));
x = rand(resSize,trainLen,size(a,2),size(Wmult,2));
for l = 1:size(a,2)
for m = 1:size(Wmult,2)
for t = 1:trainLen
u = data(t);
x(:,t+1,l,m) = (1-a(l))*x(:,t,l,m)+ a(l)*tanh( Win*[1;u] + W(m)*W*x(:,t,l,m)) ;
if t > initLen
X(:,t-initLen,l,m) = [1;u;x(:,t,l,m)];
end
end
end
end
My idea would be to first put a and Wmult into a matrix using meshgrid first
[aValue,WValue] = meshgrid(a,Wmult);
and then define a function of the kind (commented out so not to break the compiler)
%timeEvolution = @(a,Wmult)(for l = 1:size(a,2)
% for m = 1:size(Wmult,2)
% for t = 1:trainLen
% u = data(t);
% x(:,t+1,l,m) = (1-a(l))*x(:,t,l,m)+ a(l)*tanh( Win*[1;u] + W(m)*W*x(:,t,l,m)) ;
% if t > initLen
% X(:,t-initLen,l,m) = [1;u;x(:,t,l,m)];
% end
% end
% end
% end)
Is there any way to do this?

Iniciar sesión para comentar.

Respuestas (1)

Ramtej
Ramtej el 17 de Ag. de 2023
Hi Valentina,
I understand that you are trying to avoid using two for-loops to perform parameter sweep using an anonymous function.
You cannot reduce the for loops in your case using meshgrid and anonymous functions because of the following two reasons.
  1. The code provided uses both the index value "l" and the value of the vector "a" at index "l" in the inner for loops. However, you will only be passing the values "[aValue, WValue]" when using meshgrid and an anonymous function.
  2. Anonymous functions cannot contain explicit for loops and if clause statements.
Hope this helps!

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by