nearest(100*rand()) does not work

My professor wants us to utilize nearest(100*rand()) when she types this code out it works but when I utilize it I get a "Check for incorrect argument data type or missing argument in call to function 'nearest'." Why does it work for her but not me?

7 Comments

According to the doc the input needs to be an fi data type, not a double data type. Can you use round( ) or fix( ) instead?
What are the results of the following check?
which nearest -all
/MATLAB/toolbox/fixedpoint/fixedpoint/nearest.m /MATLAB/toolbox/matlab/graphfun/@digraph/nearest.m % Shadowed digraph method /MATLAB/toolbox/matlab/graphfun/@graph/nearest.m % Shadowed graph method
I'm assuming that the fixedpoint toolbox version is the one that you're after. If you don't have the FPT, or if you have some other function called which.m, you'll need to straighten that out.
In the case that you don't have FPT, you can always roll your own any number of ways.
a = [-100 100]*rand()
a = 1×2
-83.7818 83.7818
b = floor(a+0.5)
b = 1×2
-84 84
@James Tursa It seems to work fine for me in R2016b and here as well
nearest(a)
ans = 1×2
-84 84
At least in the version I have, the version of nearest.m in the FPT gives no indication that it only supports fi objects.
the a = [-100 100]*rand() worked AND b = floor(a+0.5) worked but nearest(a) gives me the same error. those 3 commands with /MATLAB don't do anything. I get an invalid use of operator error if I copy paste your first 3 lines of information
Start with the 'which' command I gave. Run that in the command window. What does it return?
If you're getting other errors, show exactly what the code was and the full text of the error message.
I'm sorry I'm having trouble understanding. You typed these three things out
/MATLAB/toolbox/fixedpoint/fixedpoint/nearest.m
/MATLAB/toolbox/matlab/graphfun/@digraph/nearest.m % Shadowed digraph method
/MATLAB/toolbox/matlab/graphfun/@graph/nearest.m % Shadowed graph method
When I copy and paste those into my live script it doesn't do anything. I'll attach a picture.
"When I copy and paste those into my live script it doesn't do anything. I'll attach a picture."
Do not copy and paste those lines. Those lines are the output printed in the command window, which DGM showed you as examples of what to expect. Trying to run those lines will not produce anything but error messages.
The line which DGM showed you and asked you to run is this one:
which nearest -all
Paste that into the command window and press enter. Show us the output.
Thank you for the explanation, I'll attach the image for what I got back.

Sign in to comment.

 Accepted Answer

DGM
DGM on 3 Feb 2022
Edited: DGM on 3 Feb 2022
Yeah. The error is because you don't have Fixed-point Toolbox.
You can either use Matt's suggestion above, or depending on what you need, you can make your own replacement for nearest().
The suggestion I gave in the comments will replicate the behavior of nearest() for both positive and negative floating-point inputs. If the inputs are integer-class, it'll be off by one. I'm going to assume that your inputs aren't already integer-class, otherwise you'll have to test.
If all you're interested in are positive inputs, then here are a couple extra options.
For floating-point inputs:
a = -1.8:0.3:1.8;
b1 = nearest(a);
b2 = floor(a+0.5); % matches for pos or neg, but not integer-class
b3 = round(a); % matches for positive inputs only
b4 = double(int16(a)); % matches for positive inputs only
fprintf([repmat('%4.1f ',[1 13]) '\n'],[a; b1; b2; b3; b4]')
-1.8 -1.5 -1.2 -0.9 -0.6 -0.3 0.0 0.3 0.6 0.9 1.2 1.5 1.8 -2.0 -1.0 -1.0 -1.0 -1.0 0.0 0.0 0.0 1.0 1.0 1.0 2.0 2.0 -2.0 -1.0 -1.0 -1.0 -1.0 0.0 0.0 0.0 1.0 1.0 1.0 2.0 2.0 -2.0 -2.0 -1.0 -1.0 -1.0 -0.0 0.0 0.0 1.0 1.0 1.0 2.0 2.0 -2.0 -2.0 -1.0 -1.0 -1.0 0.0 0.0 0.0 1.0 1.0 1.0 2.0 2.0

2 Comments

What is fixed-point Toolbox? It sounds like an addon. If it is, is there any chance you can supply me with a link to it? I can't seem to find it and I really want this to work like my professor wants it. Thank you for the answer.
Oh I guess they call it "fixed-point designer" instead of toolbox.
If it were me, I'd make reeeeal sure that it were necessary before I spent any money on addons for a class. People in uni tend to just use all sorts of functions without awareness of their dependence because their institutional license includes all the bells and whistles already.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 2 Feb 2022
Edited: Matt J on 2 Feb 2022
If you're just trying to generate a random integer from 0 to 100,
randi([0,100])
ans = 61

Categories

Find more on Argument Definitions in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 2 Feb 2022

Commented:

DGM
on 3 Feb 2022

Community Treasure Hunt

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

Start Hunting!