Error:- Index in position 1 is invalid. Array indices must be positive integers or logical values.

1 view (last 30 days)
I am getting this error during run.
irradiance matric size 100 X 360 X 200,
xMoved matric size 100 X 360 X 200,
yMoved matric size 100 X 360 X 200,
zMoved matric size 100 X 360 X 200,
function [ irradianceReorder ] = reOrderNew(irradiance, xMoved, yMoved, zMoved)
intensSize = size(irradiance);
irradianceReorder = zeros(401,401,201); %tenths
for i = 1:intensSize(1)
for j = 1:intensSize(2)
for k = 1:intensSize(3)
a = xMoved(i,j,k);
b = yMoved(i,j,k);
c = zMoved(i,j,k);
% a1 = (a*100)+1001; %hundreths
% b1 = (b*100)+1001;
% c1 = (c*100)+1;
a1 = (a*10)+101; %tenths
b1 = (b*10)+101;
c1 = (c*10)+1;
irradianceReorder(round(a1),round(b1),round(c1)) = irradiance(i,j,k) + irradianceReorder(round(a1),round(b1),round(c1)); %tenths, need round so indeces are not 1.00, 2.00 etc...
% intensity(a+11,b+11,(c*10)+1) = intens(i,j,k) + intensity(a+11,b+11,c+1); %whole numbers
end
end
end
end
Please help me to get rid of this.

Answers (2)

Guillaume
Guillaume on 1 Jul 2019
Edited: Guillaume on 1 Jul 2019
Note that the whole code can be simplified to:
function [ irradianceReorder ] = reOrderNew(irradiance, xMoved, yMoved, zMoved)
irrandianceReoder = accumarray(round([XMoved(:), YMoved(:), ZMoved(:)] .* 10 + [101, 101, 1]), irradiance(:), [401, 401, 201]);
end
However, this will produce the same or similar error.
Your function assumes that any value in round(yMoved*10+101) or round(yMoved*10+101) or round(zMoved*10+1) is positive and greater than 1 (and smaller than 401 or 201 for zMoved). Clearly, that's not the case, so you need to check the values of these arrays or change your fomula for calculating the indices.
What are you trying to do with that function anyway?

Basil C.
Basil C. on 1 Jul 2019
Hi Yash,
This could be because of the line
irradianceReorder(round(a1),round(b1),round(c1)) = irradiance(i,j,k) + irradianceReorder(round(a1),round(b1),round(c1));
If the value of a1 is less than 0.5 then using 'round' rounds off to the lower value
>>round(0.4)
ans =
0
>>round(0.6)
ans =
1
You could however try using 'ceil'
>> ceil(0.4)
ans =
1
>> ceil(0.6)
ans =
1

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!