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

조회 수: 3 (최근 30일)
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by