1

I am a complete beginner at Octave! I am struggling to figure out the minimum value from the array mse_array. I keep getting an error saying: "subscript indices must be either positive integers less than 2^31 or logicals"

Could someone help me? Here's my code:

function randomLines(data1, data2)
mse_array = []
A = rand(2,10)*100
 for i = columns(A)
    max = A (1, i) 
    min = A (2, i)
    m = (max-min)/0.16
    p = [m min];
    array_function_values = polyval(p,data2);
    current_mse = mseFunction(data1,array_function_values);
    mse_array(end + 1) = current_mse
 endfor
min_value = min(mse_array)
endfunction
2
  • 1
    In which line are you getting the error ? Commented Apr 28, 2015 at 13:40
  • 1
    Please provide the full error message. Also, please specify what mseFunction is and example inputs to your function randomLines that generate the error so that we can replicate it ourselves. Commented Apr 28, 2015 at 16:18

2 Answers 2

2

Q: How to find a minimum from an array in Octave?

octave has built-in self-describing documentation to use once in doubts

A: Always re-read both help min and doc min

octave-3.2.4.exe:1> help min
`min' is a function from the file C:\Octave\3.2.4_gcc-4.4.0\libexec\octave\3.2.4\oct\i686-pc-mingw32\max.oct

 -- Loadable Function:  min (X)
 -- Loadable Function:  min (X, Y)
 -- Loadable Function:  min (X, Y, DIM)
 -- Loadable Function: [W, IW] = min (X)
     For a vector argument, return the minimum value.  For a matrix
     argument, return the minimum value from each column, as a row
     vector, or over the dimension DIM if defined.  For two matrices
     (or a matrix and scalar), return the pair-wise minimum.  Thus,

          min (min (X))

     returns the smallest element of X, and

          min (2:5, pi)
             =>  2.0000  3.0000  3.1416  3.1416
     compares each element of the range `2:5' with `pi', and returns a
     row vector of the minimum values.

     For complex arguments, the magnitude of the elements are used for
     comparison.

     If called with one input and two output arguments, `min' also
     returns the first index of the minimum value(s).  Thus,

          [x, ix] = min ([1, 3, 0, 2, 0])
             =>  x = 0
                 ix = 3

     See also: max, cummin, cummax

Re-defining (thus rendering un-usable) elements as reserved words just increases troubles. Try to use your own naming convention, that will not harm the rest of the system -- i.e. anArrayMinimumVALUE or aSmallestVALUE, rather than "killing" the Octave function min() by creating a variable named by accident as "also" min

Sign up to request clarification or add additional context in comments.

Comments

2

There are several things wrong with your code. Here are a few to get you started.

The error is probably occurring at this line:

min_value = min(mse_array)

You're using mse_array as an index into min, but the values in mse_array are likely to valid array indices as the error states. (I'm guessing at this purely based on the name of the variable, mse_array, which appear to be from some form of mean squared error calculation.) It's important to check the line number and everything else in error messages.

Another issue in that your for loop only evaluates once. The columns function will return the scalar number of columns of A. Thus your your loop declaration is equivalent to:

for i = 10

In other words, the code will only look at the last column of A. You probably want to use:

for i = 1:columns(A)

Lastly, it's a bad idea to overwrite the built-in min and max functions in Octave with your own variable names. If you overwrite the built-in min function, you won't be able to use it anymore directly until you call clear min (alternatively, you can call builtin, but you should avoid that). Instead, just choose better variable names.

2 Comments

It's the "lastly"-part that is most important. The OP appears to intend using min as a function in the min_value-line.
@Jonas: Possibly. The min function has already been overloaded in the for loop above. It's not necessary to change the name of the variable for this code to "work".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.