Create a matrix with a for loop
古いコメントを表示
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 件のコメント
per isakson
2018 年 2 月 11 日
Replace
isreal(s) == 1
by
isreal(s)
John BG
2018 年 2 月 18 日
this doesn't solve the problem
per isakson
2018 年 2 月 18 日
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
採用された回答
その他の回答 (1 件)
Walter Roberson
2018 年 2 月 18 日
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 件のコメント
Walter Roberson
2018 年 2 月 18 日
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
カテゴリ
ヘルプ センター および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!