MATLAB: Create a matrix with a for loop

matrix

Hey there!
I am attempting to create a matrix which has n, s, k, and i as columns. However, I keep getting this error no matter how I try to make the matrix:
Subscript indices must either be real positive integers or logicals.
Is there a way around this problem considering that I need to make a list that comprises of numbers that aren't positive integers?
for i = -3:.5:2
k = 10^i;
n = (100 / k);
s = sqrt((-.5^2 .* ((100 / k)-(.5.*(100 / k)))./(100-(100 / k)))./((((100 / k)-(.5.*(100 / k)))./(100-(100 / k)))-1));
if (0<s) && (s<=1)
s = s;
elseif s > 1 || (isnan(s)==1)
s = 1;
elseif isreal(s) == 1
s = 0;
else
s = 0;
end
RTIlogk{i} = [i k n s];
end

Best Answer

Hi Colin
this is John BG jgb2012@sky.com
1.
is there need for the for loop?
i = -3:.5:2;
k = 10.^i;
n = (100 ./ k);
s = sqrt((-.5^2 .* ((100 ./ k)-(.5.*(100 ./ k)))./(100-(100 ./ k)))./((((100 ./ k)-(.5.*(100 ./ k)))./(100-(100 ./ k)))-1));
2.
NaNs to 1
s(find(isnan(s)))=1
3.
abs(s)>1 to 1, instead of
if s >1
get the indices of s meeting constrain abs(s)>1 with command find
s(find(s(abs(s)>1)))=1
4.
if you want the previous 2 constraints combined then, instead of
if s > 1 % || (isnan(s)==1)
s = 1;
use
intersect(find(s(abs(s)>1), find(isnan(abs(s)))
5. instead of
isreal(s)
use
s(find(imag(s)==0))=0
6.
instead of
if (s>0) && (s<=1)
s = s;
use
intersect( s(abs(s)>0), s(abs(s)<=1))
but wouldn't it be just fine to leave these remaining as they are?
7.
the last
else
s = 0;
would zero useful data, omitted
so all together
i = -3:.5:2;
k = 10.^i;
n = (100 ./ k);
s = sqrt((-.5^2 .* ((100 ./ k)-(.5.*(100 ./ k)))./(100-(100 ./ k)))./((((100 ./ k)-(.5.*(100 ./ k)))./(100-(100 ./ k)))-1));
s(find(isnan(s)))=1 % NaNs to 1
s(find(s(abs(s)>1)))=1 % abs(s)>1
s(find(imag(s)==0))=0 % zero reals
.
for the generation of RT do you want to use
[i k n abs(s)]
or
RTIlogk= [i' k' n' abs(s)']
RTIlogk =
1.0e+05 *
Columns 1 through 3
-0.000030000000000 0.000000010000000 1.000000000000000
-0.000025000000000 0.000000031622777 0.316227766016838
-0.000020000000000 0.000000100000000 0.100000000000000
-0.000015000000000 0.000000316227766 0.031622776601684
-0.000010000000000 0.000001000000000 0.010000000000000
-0.000005000000000 0.000003162277660 0.003162277660168
0 0.000010000000000 0.001000000000000
0.000005000000000 0.000031622776602 0.000316227766017
0.000010000000000 0.000100000000000 0.000100000000000
0.000015000000000 0.000316227766017 0.000031622776602
0.000020000000000 0.001000000000000 0.000010000000000
Column 4
0.000002887714078
0.000002889799069
0.000002896422232
0.000002917670114
0.000002988071523
0.000003249532852
0
0
0
0
0
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance for time and attention
John BG