Parameter sweep with meshgrid using a function containing a for loop
조회 수: 7 (최근 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!