0

After using:

nb = NaiveBayes.fit(training, class)

To create a Naive Bayes classifier object, I want to save N-by-D of these objects in a matrix. I have tried to do the following

ARRAYOFNAIVEBAYES(2,3) = nb;

But I get: "Error using NaiveBayes/subsasgn (line 9) The NaiveBayes class does not support subscripted assignments."

How would it be possible to fill a matrix of Naive Bayes classifiers in MATLAB?

Note that using fitNaiveBayes or fitcnb resuts in the same problem as they both return the same kind of object.

Thank you

Stack Player
  • 1,470
  • 2
  • 18
  • 32
  • try to use a cell array. initialize like this: `ARRAYOFNAIVEBAYES = cell;` then use `ARRAYOFNAIVEBAYES{2,3} = nb;` – A. Donda Mar 29 '15 at 14:40
  • ARRAYOFNAIVEBAYES = cell; ARRAYOFNAIVEBAYES(2,3) = cell; or even ARRAYOFNAIVEBAYES{2,3} = cell; all give the error Error using cell. Not enough input arguments. Any thought? – Stack Player Mar 29 '15 at 20:48
  • 1
    Sorry, I should have tested my code. See answer. – A. Donda Mar 30 '15 at 19:14

1 Answers1

0

Try to use a cell array.

First, initialize the cell array. If you now the numbers of elements you want to store, e.g. N x D, use

ARRAYOFNAIVEBAYES = cell(N,D);

If you do not know the size beforehand, you can simply start with an empty cell array:

ARRAYOFNAIVEBAYES = {};

Then if later you have generated your classifier object nb and you want to store it under the indices (2, 3), use

ARRAYOFNAIVEBAYES{2,3} = nb;

To access that value later, use the same syntax ARRAYOFNAIVEBAYES{2,3}.

For more information, see Matlab's documentation of cell arrays.

A. Donda
  • 8,381
  • 2
  • 20
  • 49