What does the tilde symbol after an equals do? [duplicate] - matlab

This question already has an answer here:
Matlab Operators
(1 answer)
Closed 9 years ago.
What does the tilde symbol ~ do in Matlab?
for example, I've got the Matlab line, in which a and b are tables.
a =~b;

Basically a is assigned to the result of the logical not operator applied to b
For example if b is the matrix
b = [12 0 10]
then
a = ~b
a = [0 1 0]
See http://www.mathworks.co.uk/help/matlab/ref/not.html for details

Related

What is the function of comma and colon together? [duplicate]

This question already has answers here:
The meaning of colon operator in MATLAB
(2 answers)
Closed 4 years ago.
In this code:
hdrMat(ctr,:) = [double(frameCtr) double(numBins) binLength Fs Fc RangeOffset];
FrameMat(:,ctr) = data;
What is the meaning of (ctr,:) and (:,ctr) in terms of vectors?
The (ctr,:) means you are addressing the ctr'th row, starting from the first row as row nr. 1. The ":" states, that you are addressing the whole row and not just an element.
The (:,ctr) means you are addressing the ctr'th column and again " : " tells matlab to address the entire column.
Example:
A = [1 2 3; 4 5 6; 7 8 9];
A(2,:) = [0 1 0]
%Output
[1 2 3]
A = [0 1 0]
[7 8 9]
You can also apply the colon operator " : " to address a certain range of the row/column by writing:
A(2:3,1)
%Output
[0; 7];
Id strongly recommend you looking into the basic matlab questions on StackOverflow and also on the MatLab official documentation, where lots of examples are given.
Cheers, Pablo

How to evaluate a matlab symbolic expression properly? [duplicate]

This question already has an answer here:
Evaluate symbolic expression
(1 answer)
Closed 4 years ago.
Lets say I have a matrix like this:
syms p;
K = [p^2+3 0; 2 5*p];
p_initial = 2;
Whats the proper/fastest way of getting K(p_initial), that is the resulting matrix if I insert 2 for p. Further, I want the resulting matrix to be of type double, not of symbolic type.
Thanks in advance
Use subs to substitute variables in symbolic expressions
subs(K,'p',p_initial)
ans =
[ 7, 0]
[ 2, 10]

Finding value that is similar and available in another vector [duplicate]

This question already has an answer here:
Matlab, finding common values in two array
(1 answer)
Closed 5 years ago.
Let say we have 2 vectors of A and B,
A=[1;2;5;6;7;9]; B=[1;3;4;7];
How to find value C that are available in both A and B? The expected value should be
C=[1;7]
Since the title of your question says "similar", I assume you want to compare with a given tolerance. For that you can use ismembertol:
tol = 1e-3;
A = [1; 2 ; 5 ; 6 ; 7 ; 9];
B = [1.0001; 3.0001; 4.0001; 7.0001];
ind = ismembertol(A, B, tol);
C = A(ind);
Very simple:
A=[1;2;5;6;7;9];
B=[1;3;4;7];
C=intersect(A,B)

Select element from array that choose in other boolean array matlab [duplicate]

This question already has an answer here:
Linear indexing, logical indexing, and all that
(1 answer)
Closed 6 years ago.
Locked. There are disputes about this question’s content being resolved at this time. It is not currently accepting new answers or interactions.
I have arrays at the same size:
a = 5:10;
b = [1 0 1 1 0 0];
I want to select the element where in the boolean array (b) is 1.
c = [5 7 8];
I want to do it in elegant way without loop.
You can just do c = a(logical(b)) if b is not already logical. If it is, then just c = a(b).

Interleaved repmat [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Element-wise array replication in Matlab
I have a m x 1 vector that I would like to repeat n times to create a (m*n)x1 vector. If I use repmat, I get something like
>> V = [a;b;c];
>> repmat(V,2,1) % n = 2, m = 3
a
b
c
a
b
c
What would be a one-line (and hopefully fast) way of getting the vector
[a;a;a;b;b;b;c;c;c]
for arbitrary n and m?
V=[ 1;2;3];
reshape(repmat(V',3,1),[],1)
ans =
1
1
1
2
2
2
3
3
3