Subscript indices must either be real positive integers or logicals
Mostra commenti meno recenti
Hei All,
I have been getting the error message "Subscript indices must either be real positive integers or logicals. " referring to the calculation of z(i, j) whilst working on the code below. I have spent hours trying to fix the issue, but couldn't figure out what I was missing.
a = 0.2302;
N=4;
theta = linspace(0, 2 * pi * pi, N);
r = linspace(a, 2 * a, N);
sigma_TT = r.^4 .* cos(2 .* theta);
x = zeros(N, N);
y = zeros(N, N);
z = zeros(N, N);
for i = 1:N
for j = 1:N
x(i, j) = (cos(theta(i)))' * r(j);
y(i, j) = (sin(theta(i)))' * r(j);
z(i, j) = real(sigma_TT([theta(i), r(j)]));
end
end
The for loop part is actually what I trying to convert from python to MATLAB. In python it is expressed as:
x = r * np.cos(theta[:,None])
y = r * np.sin(theta[:,None])
z = sigma_TT(SHmax, Shmin, Pp, Pm, a, r, theta[:,None])
Any help on this issue would be much appreciated.
4 Commenti
Ammar Dodin
il 16 Ago 2016
The reason why you see that error is because theta(i) is evaluating to zero. Then when you do sigmaTT([theta(i),r(j)]), that expression throws the error that you're seeing.
RG
il 16 Ago 2016
Ammar Dodin
il 16 Ago 2016
Modificato: Ammar Dodin
il 16 Ago 2016
I am not too familiar with numpy, but I think I understand what the first two lines of your python code are doing. It looks like you're creating new vectors that contain the cosine/sin of each of the values of the vector theta after multiplying them with some constant. I don't understand what z is doing? I am assuming sigma_TT is some function you wrote.
Could you possibly provide the Python code for that function?
RG
il 17 Ago 2016
Risposta accettata
Più risposte (1)
Image Analyst
il 16 Ago 2016
0 voti
It's pretty well explained in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F
4 Commenti
Image Analyst
il 16 Ago 2016
Did you read the FAQ? Or even see my answer? OK, here's the reason
[theta(i), r(j)]
takes on the values 0 and 0.2302. It's a 1 by 2 row vector.
You have defines sigma_TT as a 1 by 4 array with these values:
sigma_TT =
0.0028081563033616 0.0073593799306762 0.00812938329944458 -0.0093006967826725
To access those values you can pass in an index of 1, 2, 3, or 4. For example, sigma_TT(2) would get you 0.0073593799306762.
However neither 0 nor 0.2302 is of a value 1,2,3, or 4. You can't have the zeroeth index of an array. You can't have the 0.2302th index of an array. You can have the first index or second index, but not something in between. Indexes has to be positive integers like 1,2,3,4, etc. or else logical/binary arrays of true and false elements.
RG
il 17 Ago 2016
Image Analyst
il 17 Ago 2016
I thought my comment would explain it more thoroughly than his original 2-line comment. Anyway, hopefully you looked over the FAQ and maybe found some other good stuff in there.
RG
il 17 Ago 2016
Categorie
Scopri di più su Startup and Shutdown in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!