Parameter sweep with meshgrid using a function containing a for loop
6 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
KSSV
2023 年 6 月 28 日
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.
回答 (1 件)
Ramtej
2023 年 8 月 17 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!