MATLAB - transpose and extract a matrix in a line - matlab

old_mat = [1,2,3; 4,5,6];
new_mat = old_mat'(2:end, :);
new_mat = (old_mat')(2:end, :);
I would like to transpose and extract a matrix but I fail with these tries.
Is it possible to do them in a line?

Parentheses ( ) should usually[1] be the last thing in a Matlab expression. This is why an expression like a(1)(1) will give the error:
Error: ()-indexing must appear last in an index expression.
And your examples give the error
Error: Unbalanced or unexpected parenthesis or bracket.
You should, as rahnema1 suggested, extract the columns and transpose rather than trying to transpose and extract rows.
new_mat = old_mat(:, 2:end).'
Note, I've used .' which is shorthand for transpose, not ' which is shorthand for ctranspose and should be avoided unless specifically required!
[1] There are always exceptions to the rule! Here are examples of where you can put things immediately after ).
Referring to table columns or struct fields by a string, where T.('var')(1) and T.var(1) are equivalent.
Use the dot operator, which is again a feature of using structs, like S(1).var.
Generally though, if you're trying to add code next to a closing ) for simple matrix operations then there is probably a syntax error.

Related

What does VAR(:,:) mean in Matlab?

I am trying to understand the following code I found online and am having trouble with the first line:
errors(:,:) = (errors(:,:)*trials + 0.5)./(trials + 1);
PBIerr = (errors(:,2)-errors(:,3))./(errors(:,2)+errors(:,3));
What does (:,:) stand for in Matlab? Is it some kind of "select all"?
Thanks in advance!
Yes. The colon operator is a "select all, from this index" operator. Note that in the first line, its unnecesary as long as errors was already 2D, as "select the entire variable" is not something you need to specify, its by default. So it could have been
errors = (errors*trials + 0.5)./(trials + 1);.
If errors had more dimensions than 2, then the colon operator in the right hand side of the equal sign is doing something, in particular "A(:,:) reshapes all elements of A into a two-dimensional matrix. This has no effect if A is already a matrix or vector.". The one in the left hand side is useless anyway, as that line overwrites the variable.
The use of the colon operator in the second line however is well justified.
There are few other things that the colon operator means in MATLAB, read them all here: https://uk.mathworks.com/help/matlab/ref/colon.html

How to use a FOR loop in a 'struct' subindex? - MATLAB

I have a struct as shown in picture, and I need to address one of the columns in a FOR loop, as shown. But I keep getting this error:
Function 'subsindex' is not defined for values of class 'struct'.
Error in analisa_arx_teste (line 351)
In my case, what i want is :line 1 represents i = 1, line 2 i =2; So, for features, When I ask for pref_estemod(i).features is to get the values from the field features associated to each model.
I am just starting programming with matlab, so all your help would be appreciated.
Thanks!
for i=pref_estemod(1:npreferred)
[m,n]=size(Training);
features=(pref_estemod(1,i).features);
end
The error lies in i=pref_estemod(1:npreferred).
If you intend to use i for indexing, the syntax is for i=1:npreferred.
1:npreferred itself expands to the horizontal array [1,2,...,npreferred]. = with a leading for is a special syntax combination. It means do the following code with i=1, i=2, ..., i=npreferred. Now I am sure you already know the idea behind for loop. The reason I write all this is to give you the following warning/advise. Do NOT expect the same syntax to work with non-numeric arrays. Because it works in some cases and not others.

bsxfun() Invalid output dimensions

I have a function that takes upto seven arguments and returns a row vector. The first three arguments are vectors (column, column, row) and the remaining four are optional scalars.
I want to use bsxfun() to apply the function to a vector of its last argument. Below is my attempt to do that.
o = #(m,pulse,N0,samples_per_pulse,sample_select,filter,channel_cutoff) ELE452Functions.EvaluateBER(m,pulse,N0,samples_per_pulse,sample_select,filter,channel_cutoff);
oo = #(m,pulse,N0,samples_per_pulse,sample_select,filter,channel_cutoff) bsxfun(#(N0,channel_cutoff) o(m,pulse,N0,samples_per_pulse,sample_select,filter,channel_cutoff), N0' , channel_cutoff);
when I try to call the function with a vector, oo(m,pulse,N0,1,1,1,[0.5 0.2]); for example, I get this error:
Error using bsxfun
Invalid output dimensions.
I am not experienced in using bsxfun and I tried to follow the documentation.
Update:
May be this is a clearer way to ask my question:
I want to use bsxfun to rewrite (improve) the code below with out a loop.
for i=1:length(channel_normalized_cuttoffs)
BER_LPchannel(i,:) = ELE452Functions.EvaluateBER(m,pulse,N0,1,1,1,channel_normalized_cuttoffs(i));
end
The idea behind bsxfun is to evaluate a certain function for all possible combinations of two elements (b in bsxfun stands for binary), each coming from one of the arrays. (NB: This is valid if you use it with a row and a column vector. But bsxfun can also do more.)
What you want to achieve is simply: For all entries of a single array, evaluate a function.
So bsxfun is just not the correct choice here.
You could use arrayfun instead, but this still may not perform a lot better than your original for loop, as it looks like the Matlab JIT Compiler would be able to optimize most of it, considering it's simplicity.
As I don't have the code of your function, I'm not able to test it, but your solution might look a lot like this:
evalBER = #(CNcutoffi) ELE452Functions.EvaluateBER(m,pulse,N0,1,1,1,CNcutoffi);
BER_LPchannel = arrayfun(evalBER, channel_normalized_cuttoffs, 'UniformOutput', false)

What does this symbol ~ do in matlab

I am student in physics and they gave me a program in matlab to get some results. Is some point matlab crashes and indicates the problem at this line:
[~,idx] = min(cf(:));
error message is:
Expression or statement is incorrect--possibly unbalanced (, {, or [.
I want to ask what does ~ do in matlab? At my search in google I found that ~ is the approximately symbol. So what reason does it have to be there?
The tilde in that expression is used to ignore the first return value from the min function. That syntax has only been around for a few years, it's possible the error is occurring because you're using an older version of MATLAB.
Try replacing the ~ with idx. That'll cause the second return value to overwrite the first one, and will be functionally equivalent to the code you've posted.
In other contexts, ~ is the logical not operator and ~= is a logical comparison operator for testing inequality.
min function can return two values, where the first is the minimum value of the input array and the second is the index corresponding to the minimum value. Sometimes you don't really need the actual minimum value, so for the first return value you just put ~ there as a place holder, without assigning it to a specific variable.
The error is probably associated with an unbalanced statement in preceding lines of the function call you are showing

plot error in matlab Unbalanced or unexpected parenthesis or bracket

I want plot this function in Matlab:
F(p)=((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p))))
I tried to make the plot as described in this Mathworks page. And I wrote this :
p=0.001:0.001:10;
F=(((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p)))));
plot(p,y)
but I got an error:
??? F=(((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p)))));
|
Error: Unbalanced or unexpected parenthesis or bracket.
I tried also to some loop like this:
p=0.01:0.01:10;
F=zeros(1,length(p))
for i = 1:1000
F(i)=(((3/2)*(7.02^2))-(2*18*p(i)((1-(p(i)/18))*(1-(exp(-18/p(i))))));
end
plot(p,y)
but I got the error :
??? F=(((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p)))));
|
Error: Unbalanced or unexpected parenthesis or bracket.
??? Error: File: Untitled2.m Line: 4 Column: 70
Unbalanced or unexpected parenthesis or bracket.
I don't understand where the problem is...
You clearly have either too many ( or too many ). Count them, and use the Matlab editor's syntax highlighting and help to find where exactly.
You have a few issues here. The first is the number of ( and ) that you are using. The second is that you aren't accounting for the fact that p is a vector and that requires .* and ./ operators since matrix operations aren't always as simple as saying A times B when the vector lengths don't match.
Either way, this should work for you now...
F=(((3/2)*(7.02^2))-(2*18*p.*(1-(p./18))).*(1-(exp(-18./p))));
In the future it helps to just simple count the number of ( and ) like #rubenvb mentioned. That will give you a great indicator of where the issue might be.
In this particular case you would have gotten the matrix dimension mismatch error due to what I stated about the multiplication and division above.
Hope that helps.