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
Related
I am trying to write a function which returns a value 1 if the number entered is a power of two, else it returns a value 0.
function val = func_1(num)
while not(num == 1)
if num%2~=0
val=0;
break
end
num=num/2;
val=1;
end
end
But unfortunately, the function always return a value 1. Is there any error in the algorithm or the code? Thanks in advance for any help.
In Matlab, % is the comment character. Everything from that point onwards until the end of the line is ignored. This means that this line if num%2~=0 is not doing what you think it does.
Instead, use the mod function. link. In this case, the line in question becomes if mod(num, 2)~=0.
On a separate note, I suspect there is a much more efficient way of doing this. See, for example, here.
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.
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.
What does following code do in Matlab? I searched documentation but ~ shows logical not. But I could not relate following output to anything about logical not.
[~, k ] = max([0.9 1.5 4.6; 3.31 0.76 5.4]
Output: 2 1 2
The ~ placeholder allows you to ignore an output from a function. Using this allows you to acknowledge that something is output by the function, but you do not have to allocate a variable to store the output in.
When a function returns values in Matlab the number of parameters it returns and the order of these parameters is important and allows you to know what each returned value is. You may sometimes come across situations where a function returns more values than you are interested in, and you can ignore the ones you are not interested in using ~.
In your example, M = max([0.9 1.5 4.6]) would return only the maximum value. If you want to know the index of the maximum value, you have to use [M,I] = max([[0.9 1.5 4.6]). If you need to know the index of the maximum value but are not interested in the actual value itself, you can use [~,I] = max([0.9 1.5 4.6]), and you thus do not need to allocate a variable to hold the maximum values.
The according reference you're looking for is the Symbol Reference, which states:
Tilde — ~
The tilde character is used in comparing arrays for unequal values,
finding the logical NOT of an array, and as a placeholder for an input
or output argument you want to omit from a function call. Not Equal to
...
Argument Placeholder
To have the fileparts function return its third output value and skip
the first two, replace arguments one and two with a tilde character:
[~, ~, filenameExt] = fileparts(fileSpec);
which is what #David suggested in his comment.
Title said it the most, but more specifically the question begin asked is...
"Your function should keep track of the number of times that it has been called
Your function will accept 1, 5 or 6 arguments and return 1, 2 or 3 values
All arguments must be either a scalar or a row matrix; you should check for this and print
an error message and return with a 0 in the first return value if it is not true.
All arguments must be the same size: either they all must be scalars or they all must be
row vectors of the same length. You must check for this and print an error message and
return with a 0 in the first return value if it is not true"
Thats not the whole problem i assure you, but the part at which i struggle with the most. As in, i have no idea how to keep track of the number of times it's been called (with count maybe?) or have any idea how to check the argument whether or not this is a scalar or a row matrix. Also checking whether or not if they are the same size
I search up on how to do all this, no result. So therefore, i am going to assume this is not the basics.
This is all basic stuff you just didn't search hard enough:
Keep track of the number of times with a global variable. Just increment it each time the function is entered. Alternitivly you can get a workspace variable with assignin and eval: HERE for Global. HERE for assignin. HERE for eval.
Check input arguments with nargin which can be used to pass the inputs: HERE
Differing number of outputs with varargout: HERE
Use size or length to check the length of inputs. disp to display a message, set the first output and use return to return.
Hope this helps.