Array indices must be positive integers or logical values

I am training a neural network. When I execute this code in MATLAB software, it goes very smoothly. However, when I convert it to an exe for execution, I encounter this error when outputting "Array indices must be positive integers or logical values", but my output value must have negative numbers and floating point numbers, such as [0.06235, 0.3999,-0.0493]. How can I change the type so that I can continue the operation?
*This AI model does not apply to the round function. If the output value is changed to an integer, there will be problems with the operation below.
output = net(normalizedFeatures'); %<------- There's something wrong with this line
diff_0 = abs(output - 0);
diff_1 = abs(output - 1);
output_final = min(diff_0, diff_1);
output_final(diff_1 < diff_0) = 1;
output_final(diff_0 <= diff_1) = 0;

11 Comments

Please share the full error message (all the red text).
This error indicates that somewhere you are trying to index an array using decimal or negative numbers (or zero). You must use ineger values between 1 and N, where N is the number of elements along the indexed array dimension.
Here's an example.
A = [2 4 6];
A(2)
ans = 4
A(0.635)
Array indices must be positive integers or logical values.
This error occurs when I execute the exe file.
Please attach TXT_GUI.m to your post using the paperclip icon.
Interesting. Line 127 in TXT_GUI.m is commented out. Is this the same file you are using in your executable?
Sorry, I deleted a line before uploading the file, so the error should be displayed on line 126, thank you
Hi, is net a function you create or is it a matlab function. Sometimes it can happen that during compilation the wrong function is implemented, e.g. matlab function net() - Generate quasirandom point set. I observed this using the classify function. regards
... or is 'net' one of any number of unknown variables which were imported from a matfile on the prior lines? At a glance, it seems the answer would be yes.
Assuming that 'net' is an array, then whatever 'normalizedFeatures' is, it is normalized and clamped to unit-scale. All of its values exist on the interval [0 1].
If the above assumptions and observations are accurate, then all non-maximal (i.e. ~=1) elements of normalizedFeatures constitute invalid indices into the array net. As the error message indicates, array indices must be (strictly) positive integers or logicals. There's no indication that they're logicals, so nearly the entire range of index values you've defined are invalid.
Holy crap I want to go to sleep.
First, I trained the neural network with feature values. After the training, I saved the net. Then I imported the net and used another new set of features 'normalizedFeatures' to test the neural network training results. The strange thing is that I can execute it smoothly in the matlab software. , when converted into a '.exe' executable file, an error will be displayed.
Can you please share NN_97.22.mat and one of your text files (the same ones that your executable is using)? You can attach them using the paperclip icon.

Sign in to comment.

Answers (1)

If you are attempting to compile code using MATLAB Compiler that calls load to load an object from a MAT-file and there is no indication in the code itself that MATLAB Compiler needs to package the code, the dependency analyzer may not be able to detect that it needs to include the definition of the class. This could cause it to be loaded not as an object but as a number or a struct (I'm not 100% certain which.) In this case use one of the workarounds in the "Callback Problems Due to Missing Functions" section of this documentation page, as this workflow is described by the Tip in that section of that documentation page.
I believe in the case you describe there's no indication that MATLAB Compiler needs to include the definition for the network object in the application, so your attempt to pass data into it for simulation into it doesn't call the network's overload of indexing but attempts to index into a "regular variable". You can't ask for element 0.06235 of a numeric array and that's the cause of the error.

2 Comments

If I recall correctly, if a class definition is not present at the time of a load(), then the data is loaded as a vector of uint8
I believe that's the case for a class defined in a classdef file. Classes defined using the older syntax are loaded in as a struct if the class definition isn't available IIRC.

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 22 Oct 2024

Commented:

on 25 Oct 2024

Community Treasure Hunt

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

Start Hunting!