MATLAB: Are the values ,the result of rho_k, are the same?it shouldn’t be the same

for loopMATLAB

First, i introduce the code A, this code A can help me find the minimum value when the rho_k=0.6, and the loop does the code for 100 times, i called the minimum value called meann
bd=100
AXX3=zeros(1,bd);
rho_k=0.6
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for dd=1:bd
%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%In this code, each time of loop will produce a value name cvx_optval,
%so this loop run for 100 times,theorically,it will produce 100 cvx_optval
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
c3(dd)=AXX3(dd)+cvx_optval;%i have 100 cvx_optval, i will store each cvx_optval into the c3 matrix,that is
%when dd=1, the first cvx_optval will plus the first element,0, which is in the AXX3,that's how i store the cvx_optval into the c3 matrix
%obviously,there will be 100 values in c3 matrix, that is, c3 matrix is a 1 by 100 matrix
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
allthings=c3
c3_noninf=allthings(~isinf(allthings))%because some of element in this matrix are inf, these are not what i want, so i delete them
%and remain the non-inf values, these non-inf values are also the elements of vector c3_noninf
meann=min(c3_noninf) %now, i find the minimum element in the vector c3_noninf
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So in this code, i can find the minimum value when the rho_k=0.6, and the loop does the code for 100 times, i called the minimum value called meann
Now, i want to let the rho_k become 0, 0.2 ,0.4, and 0.6, and i want to let rho_k change automatically, so i must write
for rho_k=0:0.2:0.6
...
end
So, i write the code like this below
bdc=4
lkk=1:bdc %lkk=1, 2, 3, 4
lkkspace=zeros(1,bdc)
%
for rho_k=0:0.2:0.6
code A
mini_for_constant(lkk)=meann
us(lkk)=lkkspace(lkk)+mini_for_constant
end
allthings_compare=us
theorically, us will have four different values, these are the meann of rho_k=0 , rho_k=0.2, rho_k=0.4, rho_k=0.6, and it will combine to a 1 by 4 vector called "us", however,the window show me this
us =
17.8487 17.8487 17.8487 17.8487
yes! the "us" is a 1 by 4 vector, but each element is the same, why? where do i make a mistake?

Best Answer

The original code is like this
bdc=4
lkk=1:bdc %lkk=1, 2, 3, 4

lkkspace=zeros(1,bdc)
%

for rho_k=0:0.2:0.6
code A
mini_for_constant(lkk)=meann
us(lkk)=lkkspace(lkk)+mini_for_constant
end
allthings_compare=us
However,as we can see,the the value of lkkspace(lkk) won't change with the rho_k changing,so if we want that when rho_k become 0.2,should be the "second" value of rho_k ,and there will be a value to store value in the "second" position
bdc=4
lkk=1:bdc %lkk=1, 2, 3, 4
lkkspace=zeros(1,bdc)
rho_k=[0 0.2 0.4 0.6]
%
for lkk=1:bdc
code A (the rho_k in this code A have to be modified to rho_k(lkk))
mini_for_constant(lkk)=meann
us(lkk)=lkkspace(lkk)+meann
end
allthings_compare=us
Now,we can find that us is a 1 by 4 matrix,and each element in it is the minimum scalar when the rho_k is 0,0.2,0.4 and 0.6
Related Question