Why isn't min working for me?
3 次查看(过去 30 天)
显示 更早的评论
I have been trying the examples from the documentation but those don't even work. I keep getting the error that index exceeds matrix dimensions. Does anyone know what I should do?
1 个评论
the cyclist
2011-12-15
It is pretty difficult to diagnose your problem when you don't provide any code. Can you edit your question to include a small snippet that gives the error?
回答(2 个)
the cyclist
2011-12-15
Did you accidentally define a variable called "min", and it is now being used as a variable rather than a function? For example:
min = 0; % Bad coding! Don't use keyword as variable name!
min([6 3])
will give such an error.
3 个评论
Jan
2011-12-15
@the cyclist: Small correction: "min" is not a _keyword_, but a toolbox function. See "help iskeyword" and "edit iskeyword". But the argument is correct inspite of this term. +1
Avishek
2011-12-15
be sure to use this lines of codes in all script files that u write
clear all% to clear all the variables
clc; % clear command
*suppose *
a= [1 2 3 4 5 6 7];
min_a=min(a);
% if b is a matrix
b=[1 2 3 4; 2 3 4 5;]
min_b= min(b(:));
% b(:) this command transforms b into a vector from a matrix
4 个评论
Chandra Kurniawan
2011-12-15
About the "clear all": http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Walter Roberson
2011-12-15
Avishek, you are using "clear all" without understanding what it does. Once you know what it really does, you will likely find that good uses for it are quite rare.
If you are calling a script file from the command line or from a script file, then chances are that you have created some variables already that you do not want to erase.
If you are calling a script file from a function, if you have "clear all" then everything in the function workspace (and much more!) will be deleted, which is going to cause problems for the function.
Using "clear all" from a script makes your script unusable as a utility routine to do some work, discouraging the important practice of code re-use.
With regards to space issues: when you are writing a script or function that uses variables of more than trivial size, you should get in the habit of "clear" of the individual variables once your code is finished with them.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!