MATLAB: I need to store values from a for loop with a non-integer index in matrices. The matrices I have now have random 0’s in the output.

for loopmatrices

T1 = 1800;
T2 = 100;
c_vals=0.0:.001:.05;
c_out=zeros(length(c_vals),1);
T1C_out=zeros(length(c_vals),1);
T2C_out=zeros(length(c_vals),1);
for c=0.0:.001:.05
T1C=(1000*(1/((1/(T1*.001))+(7.52*c))));
T2C=(1000*(1/((1/(T2*.001))+(41.6*c))));
c_out(find(c_vals==c,1)) = c
format shortg
T1C_out(find(c_vals==c,1))= T1C
T2C_out(find(c_vals==c,1))= T2C
end;
I need to store c, T1C and T2C values for each c value in 3 matrices am getting this as an output and I do not understand where the random 0's are coming from (at c=0.027,0.028,0.030,0.031,0.037,0.038,0.039,0.044,0.045 and their corresponding T1C and T2C values) or how to fix it. Help please!
c_out =
0
0.001
0.002
0.003
0.004
0.005
0.006
0.007
0.008
0.009
0.01
0.011
0.012
0.013
0.014
0.015
0.016
0.017
0.018
0.019
0.02
0.021
0.022
0.023
0.024
0.025
0.026
0
0
0.029
0
0
0.032
0.033
0.034
0.035
0.036
0
0
0
0.04
0.041
0.042
0.043
0
0
0.046
0.047
0.048
0.049
0.05
T1C_out =
1800
1776
1752.6
1729.8
1707.5
1685.9
1664.8
1644.2
1624.1
1604.5
1585.4
1566.7
1548.5
1530.7
1513.2
1496.2
1479.6
1463.3
1447.4
1431.8
1416.5
1401.6
1387
1372.7
1358.6
1344.9
1331.4
0
0
1292.6
0
0
1256
1244.2
1232.7
1221.4
1210.3
0
0
0
1167.7
1157.6
1147.6
1137.8
0
0
1109.3
1100.1
1091.1
1082.2
1073.5
T2C_out =
100
99.586
99.175
98.767
98.363
97.962
97.565
97.17
96.779
96.391
96.006
95.624
95.245
94.869
94.497
94.127
93.759
93.395
93.034
92.675
92.319
91.966
91.615
91.268
90.922
90.58
90.24
0
0
89.235
0
0
88.252
87.929
87.609
87.291
86.975
0
0
0
85.734
85.429
85.127
84.826
0
0
83.938
83.646
83.356
83.068
82.781

Best Answer

You should treat meta-data as data. You can either keep your 'fractional index' as a separate vector (as you have done already), or keep in the matrix itself. Which of these makes more sense depends on your usage of the data later on. The code below returns slightly different values in those vectors and gets rid of all the zeros. Please check if this is what you mean.
T1 = 1800;
T2 = 100;
c_vals=0.0:.001:.05;
c_out=zeros(length(c_vals),1);
T1C_out=zeros(length(c_vals),1);
T2C_out=zeros(length(c_vals),1);
for c_index=1:numel(c_vals)
c=c_vals(c_index);
T1C=(1000*(1/((1/(T1*.001))+(7.52*c))));
T2C=(1000*(1/((1/(T2*.001))+(41.6*c))));
c_out(c_index) = c;
format shortg
T1C_out(c_index)= T1C;
T2C_out(c_index)= T2C;
end
BTW, if this isn't example code, you can run this as a vector operation without the need of a for-loop:
T1C_out2=(1000*(1./((1/(T1*0.001))+(7.52*c_vals'))));
T2C_out2=(1000*(1./((1/(T2*0.001))+(41.6*c_vals'))));
isequal(T1C_out2,T1C_out)
isequal(T2C_out2,T2C_out)