Array indices must be positive integers or logical values
98 views (last 30 days)
Show older comments
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
Cris LaPierre
on 24 Oct 2024 at 15:35
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.
Answers (1)
Steven Lord
on 24 Oct 2024 at 17:36
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
Walter Roberson
on 24 Oct 2024 at 18:05
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
Steven Lord
on 24 Oct 2024 at 18:17
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.
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!