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

3 views (last 30 days)
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

Accepted Answer

Ridwan Alam
Ridwan Alam on 17 Dec 2019
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 Comments
Cappriece Clarke
Cappriece Clarke on 5 Jul 2021
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".

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!