Create a matrix with a for loop
Show older comments
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
4 Comments
per isakson
on 11 Feb 2018
Replace
isreal(s) == 1
by
isreal(s)
John BG
on 18 Feb 2018
this doesn't solve the problem
per isakson
on 18 Feb 2018
No but it's still a relevant comment.
the initial s is as follows:
s =
Column 1
0.000000000000000 + 0.288771407778945i
Column 2
0.000000000000000 + 0.288979906876575i
Column 3
0.000000000000000 + 0.289642223181746i
Column 4
0.000000000000000 + 0.291767011359047i
Column 5
0.000000000000000 + 0.298807152333598i
Column 6
0.000000000000000 + 0.324953285195147i
Column 7
NaN + 0.000000000000000i
Column 8
0.274222586190323 + 0.000000000000000i
Column 9
0.121267812518166 + 0.000000000000000i
Column 10
0.064418039894926 + 0.000000000000000i
Column 11
0.035623524993955 + 0.000000000000000i
if you decide to apply isreal(s), look what happens:
isreal(s)
ans =
logical
0
despite some of the values of s are clearly real, applying isreal() does not return those with null imaginary.
Instead, look for those components of s that have null imag(s), please have a look at my answer, thanks in advance
John BG
Accepted Answer
More Answers (1)
Walter Roberson
on 18 Feb 2018
Change
RTIlogk{i} = [i k n s];
to
RTIlogk{2*i+7} = [i k n s];
Or, better yet, learn this pattern:
ivals = -3:.5:2;
num_i = length(ivals);
RTIlogk = cell(num_i,1);
for i_idx = 1 : num_i
i = ivals(i_idx);
...
RTIlogk{i_idx} = ...
end
This pattern does not require that the values be equally spaced or even real valued.
2 Comments
Walter Roberson
on 18 Feb 2018
And be careful: the < operator ignores any complex component for the comparison. You should always check for complex before using the relative comparison operators.
And
1.
assert(3>4)
Assertion failed.
as expected throws error if condition false.
2.
assert does nothing if condition true:
assert(3<4)
4.
as Walter mentions above, in the following line operators < > do not compute imaginary numbers, returning false for the real parts only, and in turn assert returns error
assert(1j*3<1j*4)
Assertion failed.
assert(0+1j*3<0+1j*4)
Assertion failed.
assert(3i<4i)
Assertion failed.
5.
However, to further warn about the use of operators < > with figures that have non-null imaginary parts
assert(3i<4)
now assert does nothing, which implies that
3i<4
returns true
and
assert(3i>4)
Assertion failed.
6.
As Walter points out,
avoid using operators
<
>
with numbers that have non null imaginary parts.
apply abs() real() or any other function before using < > to precisely avoid href = ""</a> dealing with complex figures.
Regards
John BG
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!