For loop for a comparing values of a column vector with a value

11 ビュー (過去 30 日間)
Mike Mierlo van
Mike Mierlo van 2019 年 12 月 17 日
コメント済み: Cappriece Clarke 2021 年 7 月 5 日
Hi, I want to perform the following, but I get unexpected outcome. The plan is:
1: make column vector ‘costs’
2: for every iteration of ‘i’ compare every value in vector ‘costs’ with the value of ‘PM_cyc’.
3: If the value of the element in ‘costs’ < PM_cyc, than that element changes in PM.
4: If the value of the element in ‘costs’ > PM_cyc, than that element changes in CM.
5: I expect an outcome for the code below as an matrix With size 10x20. 10 rows becose every iteration cost is a column vector of 10 elements. 20 columns because of 20 iterations. The values in this matrix are PM or CM. But I get an error instead. Please help.
Fail = [95:105];
CM = 9000; %euro
PM = 2000; %euro
costs=[Fail]';
PM_cyc = 90:110;
for i = 1:length(PM_cyc)
costs(costs>PM_cyc(i))=PM;
costs(costs<PM_cyc(i))=CM;
costs(i);
end

採用された回答

Ridwan Alam
Ridwan Alam 2019 年 12 月 17 日
costs=[Fail]';
costs = repelem(costs,1,length(PM_cyc));
output = zeros(size(costs));
for i = 1:length(PM_cyc)
output(find(costs(:,i)<=PM_cyc(i)),i)=PM;
output(find(costs(:,i)>PM_cyc(i)),i)=CM;
end
  4 件のコメント
Ridwan Alam
Ridwan Alam 2019 年 12 月 18 日
Sure. Glad to help.
Cappriece Clarke
Cappriece Clarke 2021 年 7 月 5 日
Hi Ridwan Alam,
I would like to do something similar, by testing the maximum likelihood function and the wald test (separately) on each parameter in a column vector and having the results stored in a different array. Can you assist me with this please. Let's say the vector is called "colVect".

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by