This question already has answers here:
How to elegantly ignore some return values of a MATLAB function
(8 answers)
How do I get the second return value from a function without using temporary variables?
(2 answers)
Closed 9 years ago.
I'm not very good at programming and new to matlab, so sorry if I'm not using the right terminology.
If I use the e.g. fminbnd procedure in Matlab first i get the x-value which minimises and then i get the function value. Is there a neat way for me to get just the minimum function value.
To make it clear, for me it seems I have to do:
[x,y] = fminbnd(h,-10,10)
when I only need y. Is there any way for me to not get x?
Use ~ to suppress x output. Only available in later versions of matlab (=> r2009b).
[~, y] = fminbnd(h, -10, 10);
Related
This question already has answers here:
MATLAB not enough input arguments
(2 answers)
Closed 5 years ago.
In Matlab, I want to get the variables from the workspace for the function . But I did not do it.
For example; the function is:
function Y = objfun(x)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
end
gives me the following problem when I run the function
>> objfun
Not enough input arguments.
Error in objfun (line 5)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
x variable is exist in workspace like x= [4 5 7] and I don't want to write it inside of function. so what shall I do.
Maybe it is very east question for you but I don't know and I trid make it.
could you help me?
In Matlab (or Octave) you can use scripts or functions.
If you create script called objfun, you have what you are looking for. Just call it using objfun and it will use workspace variable x. The script is saved as objfun.m.
Functions are different. They can have arguments, but these arguments are local variables (only available within the function).
If you define a function, you must call it with the arguments.
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 6 years ago.
When I want to access a specific element of a matrix, I use indexing with parentheses:
m = calc_stuff(...);
x = m(index1, index2);
However, I often want to do that in one line of code, like this:
x = calc_stuff(...)(index1, index2);
How can I express it?
A specific example:
m = cumsum(rand(10,4));
x = m(10, 1);
The above script calculates some sums of random variables, and then I take one example value out of the result matrix.
How could I write it as one line? The following doesn't work:
x = cumsum(rand(10,4))(10, 1);
Error: ()-indexing must appear last in an index expression.
Here, I want a general syntax, which is applicable for any calculation, not necessarily involving random variables.
You may want to check out the "Functional Programming Constructs" on the FileExchange).
Especially the file paren.m does what you need. So you would write
x = paren( cumsum(rand(10,4)), 10, 1 );
Perhaps not as elegant as the direct "()"notation, but that is not supported in MATLAB in the way you would like to use it.
This question already has answers here:
suppressing output variables in matlab
(2 answers)
Closed 8 years ago.
For example, I have a function
[power, capacity] = function_name(users, distance, radius)
and the function call is
[~, capacity] = function_name(users, distance, 5);
~ just means that you don't want to store the result in any variable.
here is a detailed explanation:
http://www.mathworks.com/matlabcentral/answers/72537-what-does-a-tilde-inside-square-brackets-mean
And the most relevant section: "when you use [~,palette], that means that you just want the second output of your function, and do not care the first one."
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 8 years ago.
E.g. I have the output of cov(A,B), which is a 2×2 matrix.
I want to select the element in position 2,1 of the matrix.
I can do this by blah = cov(A,B) and then select blah(1,2).
This isn't the most efficient way to do it though, and I'd prefer to do it in one line. Is there a way to do that?
You can try using getfield():
getfield(cov(A,B), {1,2})
The performance difference between this and what you have currently will likely be negligible, however. I personally would prefer just using that temporary variable.
<stealing brilliance from Amro>
You can also do this:
C = builtin('_paren', cov(A,B), 2, 1);
</stealing brilliance from Amro>
This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 9 years ago.
I was asking myself if there was a quicker way to do this in matlab :
Imagine we have a 10x2 vector V and we want to use the x dimension (number of lines, here 10) in a function or do whatever we want with it. The way I usually do it is this :
[x y]=size(V);
function(x)
But would it be possible to make it differently? Soemething like
function(size(V)(1))
Thanks for your help !
MATLAB's size can take a second input argument, indicating the dimension of which you would like to know the size. The output is scalar in that case:
x = size(V,1);
y = size(V,2);
See help size for more details.