如果這個變量在調用之間發生了變化,為什麼匿名函數的固定參數沒有更新? (Why is fixed argument of anonymous function not updated, if this variable has changed between calls?)


問題描述

如果這個變量在調用之間發生了變化,為什麼匿名函數的固定參數沒有更新? (Why is fixed argument of anonymous function not updated, if this variable has changed between calls?)

開發一個優化腳本,我意識到 Matlab 並沒有像我預期的那樣處理匿名函數的輔助參數。

考慮這個簡單的 MWE:

%% Define basic parameters
daysTotal       = 3;
hoursTotal      = daysTotal*24;
nhi             = 4;        % Third argument, does not change.
intervalsTotal  = hoursTotal*nhi;
q_in_mean       = 0.82;

% Original definition of var2
var2            = zeros(hoursTotal, 1);
var2(2:2:end)   = 1;        % Second argument, does change!

var1_initial = var2.*q_in_mean/12; % Initial version of first argument

%% Define anonymous function
objFun          = @(var1) objFunModel(var1, var2, nhi);

%% Call objFun for the first time
objFun(var1_initial); % Result of sum(var2) = 36, which is correct.

%% Change var2
var2            = zeros(hoursTotal, 1);
var2(4:4:end)   = 1;

%% Call objFun again
objFun(var1_initial); % Result of sum(var2) is still 36 inside objFunModel
sum(var2) % Actual value of sum(var2) = 18 after change!

%% Separate functions
function varStd = objFunModel(var1, var2, nhi)
    sum(var2)
    varRes  = cumsum(2*var1 ‑ 0.12*var2);
    varStd  = std(varRes);
end

雖然 var2 在兩個函數調用之間發生了變化,它是 not 更新的,正如您在終端輸出中看到的那樣。這是預期的行為,還是錯誤?如果是前者,我該怎麼做才能強制 var2objFunModel 內更新?重新定義匿名函數?


參考解法

方法 1:

When you create an anonymous function, the variables that are not in the parenthesis following the @ (var2 and nhi in your example), are passed by value, not by reference. Thus, MATLAB has no way of knowing that the variable changed.

(by winkmalJAC)

參考文件

  1. Why is fixed argument of anonymous function not updated, if this variable has changed between calls? (CC BY‑SA 2.5/3.0/4.0)

#matlab #anonymous-function






相關問題

MATLAB:多線程和多核之間的區別 (MATLAB: difference between Multithreading and Multicore)

在 matlab 中用 imread 讀取圖像文件會給出什麼樣的表示? (reading a image file with imread in matlab gives what kind of representation?)

Як прызначыць індывідуальныя пазнакі значэнняў на восях у Matlab? (How to assign individual labels to values on the axes in Matlab?)

覆蓋 MATLAB 默認靜態 javaclasspath 的最佳方法 (Best way to override MATLAB's default static javaclasspath)

如何在 Matlab 中為這個 3D 繪圖設置動畫? (How to animate this 3D plot in Matlab?)

如何轉換 x = [x_de,x_nu]; 從matlab到python? (How do I convert x = [x_de,x_nu]; from matlab to python?)

opnet 和 matlab 接口 (opnet and matlab interfacing)

Matlab:使用正非整數邊緣權重執行最小切割 (Matlab: perform min cut with positive non-integer edge weights)

帶矩陣的圖像疊加 (Image overlay with matrix)

在矩形 [a,b]x[c,d] 上離散最小二乘逼近。使用函數 1,x,y,sin(x) 和 sin(y) 作為基礎 (Discete least sqauare approximation on rectangle [a,b]x[c,d].Use functions 1,x,y,sin(x) and sin(y) as the basis)

如果這個變量在調用之間發生了變化,為什麼匿名函數的固定參數沒有更新? (Why is fixed argument of anonymous function not updated, if this variable has changed between calls?)

在 MATLAB 中通過 HTTP 接收數據 (Receive data via HTTP in MATLAB)







留言討論