Find function return value in Matlab [duplicate] - matlab

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

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

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

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