MATLAB Answers

Subscript indices must either be real positive integers or logicals

2 views (last 30 days)
RG
RG on 16 Aug 2016
Commented: RG on 17 Aug 2016
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 Comments

Show 1 older comment
RG
RG on 16 Aug 2016
Thanks Ammar. I have in fact tried with non-zero values of theta and still get the same error because this time r(i) is not an integer. Is there a way to write the code for z without the loop? The extraction from python that I mentioned above is taken from a code that is functional with same inputs I am trying to use.
I am able to compute x and y without the loop, but get stuck with the syntax for z. The syntax for x and y that give me the results I expect are:
x = (cos(theta))' * r;
y = (sin(theta))' * r;
Ammar Dodin
Ammar Dodin on 16 Aug 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
RG on 17 Aug 2016
You are right, sigma_TT is a separate function. I managed to fix the issue, apparently I forgot to write sigma_TT function. I will post the solution below just for a reference.

Sign in to comment.

Accepted Answer

RG
RG on 17 Aug 2016
I had to first create a function called sigma_TT:
function sigma_TT = sigma_TT(r, theta)
sigma_TT = r.^4 .* cos(2 * theta);
The code worked well when sigma_TT is called within compute_sTT function:
function compute_sTT
a = 3;
N = 4;
theta = linspace(0, 3 * pi * pi, N);
r = linspace(a, 3 * a, N);
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) = sigma_TT(r(j), theta(i));
end
end

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 16 Aug 2016

  4 Comments

Show 1 older comment
RG
RG on 17 Aug 2016
The link you provided was already explained above by Ammar Dodin.
Image Analyst
Image Analyst on 17 Aug 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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!