How to index a temporary matrix? [duplicate] - matlab

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.

Related

Using mean function in MATLAB yielding different results [duplicate]

This question already has answers here:
Subscript indices must either be real positive integers or logicals, generic solution
(3 answers)
Closed 3 years ago.
I have just recently started using MATLAB as it's pretty good for machine learning and the like.
Currently, I am working on some type of classification which is pretty long-winded and complicated if I tried to explain everything I am trying to accomplish therefore I will just state the exact code giving me problems.
So, I am given a 1010 x 1764 single type matrix by some function. say the matrix is called train_examples_2_2 as you can see on the right-hand side of the screenshot below.
As you can also see from the screenshot above (on the right-hand side), the calls to mean and std:
mean = mean(train_examples_2_2)
std = std(train_examples_2_2)
Yield the correct results.
However, when I run the same code several times sometimes I get an error on the line mean = mean(train_examples_2_2) stating:
Array indices must be positive integers or logical values.
The exact code I am concerned with is:
mean = mean(train_examples_2_2) % <----- error appears here
std = std(train_examples_2_2)
for i=1:size(train_examples_2_2,1)
train_examples_2_2(i,:) = train_examples_2_2(i,:) - mean;
train_examples_2_2(i,:) = train_examples_2_2(i,:) ./ std;
end
% end of standardisation process
where train_examples_2_2 is provided by some function that I did not create nor can modify.
According to the MATLAB documentation:
If A is a matrix, then mean(A) returns a row vector containing the
mean of each column.
which is what I get the first time I run the code upon opening Matlab but after that, it yields the aforementioned error.
I am using MATLAB R2018b.
I'm I making a simple mistake or could this possibly be a bug?
Thanks for taking the time to help out.
unlike let's say python you shouldn't/can't/mussn't re-define function names or default variables.
mean = mean(train_examples_2_2) % <----- error appears here
matlab doesn't distinguish between the callable mean() function and the variable ```mean``. especially confusing since indexing and calling sth is done by using round brackets.
So....?
call your variable sth. other than mean. mean_ will already do the trick.

How to understand the meaning of Matlab 'end' operator inside expression parameters [duplicate]

This question already has answers here:
What are the semantics of 'end' in Matlab?
(2 answers)
Closed 7 years ago.
I have to adjust a matlab script and have a problem with this piece of code
if isOk()
h = h(1+limit:end-limit, 1+limit:end-limit, :);
limit= 0;
end
Unfortunately I haven't understood how to read the expression
h = h(1+limit:end-limit, 1+limit:end-limit, :);
what is the meaning of end operator inside it?
The end operator is just a shorthand for length(var). You can even do stuff like var(1:end/2) to get the first half of your variable.
For more than 1 dimension, the end operator acts as size(var, x), where x is the current dimension.
Attention: Matlab does not use zero based indexing, i.e. accessing array(length(array)) is actually correct. Hence the relation of end with the size of the variable is correct, since it is the last index of the variable, where the variable has one-based indexing.
So in Matlab you can even write stuff like
a= rand(4,8); a(1:end/2, 1:end/2);
If you leave all away, and just type a(:) (i.e. nothing before and after the :, then you actually mean 1:end
(:) ==> (1:end)

In Matlab, how to quickly select single element of matrix produced by a function? [duplicate]

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>

Access second output of Matlab procedure directly [duplicate]

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);

Quicker way to access a particular column of a function result in matlab [duplicate]

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.