我想避免在我的代碼中使用 for 回圈,因為它的計算量非常大。
我在資料框中搜索變數,如果變數為 0,則應將 1000 的數量添加到另一個變數中。如果變數為 1,則相同。
for i=1:height(dataframe)
if df.status(i) ==0
df.Number(i) = df.Number(i) 10000;
else if df.status(i) ==1
df.Number(i) = df.Number(i) 20000;
end
end
end
我非常感謝您的任何建議-蒂姆
uj5u.com熱心網友回復:
假設您的回圈訪問每個元素df.Number并且df.status大小相同,那么您可以將代碼總結為
df.Number = df.Number 10000 (df.status==1) * 10000;
MATLAB 中的邏輯值(布林值)的值始終為 0 或 1,比較status==1可確保邏輯值。
如果df.status是合乎邏輯的,則可以跳過比較:10000 df.status * 10000。
uj5u.com熱心網友回復:
我不知道資料框的資料型別,但您可以通過在達到該條件時獲取索引來解決此問題。
% Generate a simple dataframe
dataframe = [0 0 1 1 2 2 1 0 5]';
% Get 0s index and add 10,000 to those indexes
idx_0 = dataframe == 0;
dataframe(idx_0) = dataframe(idx_0) 10000;
% Get 1s index and add 20,000 to those indexes
idx_1 = dataframe == 1;
dataframe(idx_1) = dataframe(idx_1) 20000;
% Print dataframe variable
dataframe
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/345207.html
標籤:MATLAB
上一篇:如何更改圖形視窗的外觀?
