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

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)

Related

How to directly access functions that return multiple outputs 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 5 years ago.
I'm trying to directly retrieve the second variable of a function that returns multiple variables. For example, I have the column vector a as follows:
a = [5 ; 4 ; 3 ; 2 ; 1 ; 9 ; 8 ; 7];
And I want to retrieve the index of the minimum value. I know I can do this.
[n,i] = min(a);
i
But how can I do this essentially in one line? I thought this ould work but it does not:
min(a)(1)
You can ignore one of them like this:
[n,~] = min(a);
Or:
[~,i] = min(a);
It's not pretty, but it might work for you:
find( x == min(x), 1, 'first' )

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

use vector to select columns of a matlab array [duplicate]

This question already has answers here:
Get different column in each row
(2 answers)
Closed 8 years ago.
This is an issue i commonly find myself trying to solve. I have the following:
A = [1 2;
3 4;
5 6;
7 8;
9 10];
B = [1,2,1,2,2];
On each row (i) of A, i want to return the value of the column specified in B(i). I currently solve the problem using a loop:
result = zeros(size(B));
for i=1:length(B)
result(i) = A(i,B(i));
end
Where result = [1 4 5 8 10]
But this seems inelegant to me. Is there a one-liner?
You could get the correct linear indices using sub2ind:
rows = (1:numel(B))'
cols = B(:);
ind = sub2ind(size(A), rows, cols);
A(ind)
or in a one-liner
A(sub2ind(size(A), (1:numel(B))', B(:)))
or a more elegant method (taken from the 2nd answer to the duplicate question)
diag(A(:,B))
I can't tell you about performance though...

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

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

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