Array indexing in a function - working in R2018a but not in R2021a

I have been building a function that builds two masks (with ones and zeros) from arrays and thresholds, and add them to create a 'final' map.
The function works in R2018a but not in R2021a. I suppose this has something to do with how the array are indexed in functions. Not sure.
The error message is:
Index in position 3 exceeds array bounds (must not exceed 1).
It perefectly work as a script. Note that 'bands' is a 3D array '30314x27027x4 double'.
The function is quite simple:
function A = optim4band(B,N)
global bands
persistent final
%%create first mask - create a mask from bands(:,:,1) based on a threshold/value B(1), then we do the same below with bands(:,:,2) and B(2)
mask1=zeros(30314,27027);
mask1(bands(:,:,N(1))>=B(1))=1;
%%create second mask
mask2=zeros(30314,27027);
mask2(bands(:,:,N(2))>=B(2))=1;; %%% ERROR IS HERE !! N(2)=2, same error if I directly put 2 instead, looks like the function cannot accept 2 here. Works well as a script but not as a function.
%%add the two masks
final=mask1.*mask2;
final(isnan(bands(:,:,1)))=NaN;
[missing parts here]
A= .... calculation based on final
end

3 Kommentare

dpb
dpb am 13 Jul. 2023
Bearbeitet: dpb am 13 Jul. 2023
Having the global variable makes difficult to debug as you're discovering -- my recommendation would be to pass it to the function and remove the global declaration.
Also, there appears to be no point in making final persistent; it's recomputed every call, anyway,
It's not possible to actually debug anything without the input data and the calling sequence use, but the obvious conclusion is that at the time the error occurs that the array bands is a 2D, not 3D array. Since it's a global, its value can be changed behind your back without you being aware of where it occurred; that must have happened somewhere/somehow.
If you'll put
size(bands)
in the function before it (the array bands) is referenced, you'll find that it will return one as the third dimension despite what you think it should be...
DGM
DGM am 13 Jul. 2023
Bearbeitet: DGM am 13 Jul. 2023
This isn't the problem, but when I see something like this:
mask1=zeros(30314,27027);
I'd be wary. The page geometry of mask1,mask2 need to match the page geometry of bands, but you're presuming the page geometry instead of deriving it from bands. Unless you have some safeguard to check your assumptions, you should avoid hardcoding things like this instead of obtaining them programmatically.
[h,w,ch,~] = size(bands);
mask1 = zeros(h,w);
@DGM -- I'm open to what I've overlooked/missed, but I don't see anything else in the posted code than can generate the specific error message...now, of course, it's also possible the posted error and the code don't go together; it wasn't pasted in with the context of the actual execution, but...

Melden Sie sich an, um zu kommentieren.

Antworten (1)

That error is most likely a result of not including 'global bands' in your calling script. You appear to have only added it to your function. If it is not declared in both locations, then the variable is empty inside the script, and results in the error you are seeing when you try to index a non-singleton dimension.
bands = rand(5,5,5);
optim4band
A = [] B = [] C = []
Index in position 3 exceeds array bounds. Index must not exceed 1.

Error in solution>optim4band (line 10)
D = bands(:,:,2)
function optim4band
global bands
A = bands
B = bands(:,:,1)
C = bands(:,:,:)
D = bands(:,:,2)
end

1 Kommentar

I had realized I had left out the more obvious cause in my earlier reply and see you had provided the answer. Almost certainly the case if the code is taken out of the function and run in the base workspace, then the array is going to be there regardless of whether declared global or not. Another example of why to avoid their use unless absolutely necessary, and seem unneeded here, just pass the array to the function.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2021a

Gefragt:

am 12 Jul. 2023

Kommentiert:

dpb
am 13 Jul. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by