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

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

Related

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)

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

FOR loop over column vector vs row vector [duplicate]

This question already has answers here:
The for loop doesn't iterate
(2 answers)
Closed 9 years ago.
I was just writing a "kinda-foreach" loop in Matlab and encountered this strange behavior:
I have the matrix A:
A = [
3
9
5
0];
And I want to use a foreach loop (as explained here) on the A.
If I write this:
for i = A
disp('for')
i
end
The result will be:
for
i =
3
9
5
0
But when I use the transpose, the result will change:
for i = A'
disp('for')
i
end
Result:
for
i =
3
for
i =
9
for
i =
5
for
i =
0
Which is the result I want.
Can anybody explain what's going on here? What's the difference between these two cases?
when you type
A = [
3
9
5
0];
you create a column vector. Because Matlab iterates over columns you get one answer (the first column). By transposing it you get a row vector with 4 columns and therefore 4 answers each with one column.
In Matlab, the for loop iterates over columns. http://www.mathworks.es/es/help/matlab/ref/for.html

Find function return value in Matlab [duplicate]

This question already has an answer here:
How to determine whether a matrix is empty or not in matlab?
(1 answer)
Closed 9 years ago.
The find() function in Matlab returns and empty 1x0 matrix if it is not able to find anything for the given criteria. How to check if this is indeed the case - that the given criteria is nowhere satisfied in the matrix provided?
check with isempty
b = [ 1 2 3 5 ];
a = find( b == 4 );
if isempty( a )
fprintf(1,'not found\n');
end