Parameter sweep with meshgrid using a function containing a for loop
6 views (last 30 days)
Show older comments
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 Comments
KSSV
on 28 Jun 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.
Answers (1)
Ramtej
on 17 Aug 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.
- 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.
- Anonymous functions cannot contain explicit “for” loops and “if” clause statements.
Hope this helps!
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!