Julia dot operator and boolean - boolean

Suppose I have p = [true, true, false, false] and q = [true, false, true, false]. How can I logically "and" them, say like
p .&& q?

Use .& instead:
julia> p=[true, true, false, false]
4-element Array{Bool,1}:
1
1
0
0
julia> q=[true, false, true, false]
4-element Array{Bool,1}:
1
0
1
0
julia> p .& q
4-element BitArray{1}:
1
0
0
0
You have to be careful though as & works also on non-Bool elements:
julia> [11,12,13] .& [3,2,1]
3-element Array{Int64,1}:
3
0
1

Related

How to convert a matrix from int to bool in Julia like MATLAB's logical()?

If you have an array a of ints in MATLAB you can do logical(a) to get a Boolean array where every nonzero entry is 1 and every 0 entry is 0. How do you do this in Julia?
Another option is to use the iszero function, which gives you a clearer syntax:
julia> a = rand(0:3,2,2)
2×2 Array{Int64,2}:
0 3
0 1
julia> b = iszero.(a)
2×2 BitArray{2}:
1 0
1 0
You can find information about iszero in here or by typing ?iszero at the REPL.
The use of broadcasting is needed because that makes the function compare every element to zero. If you don't use it, it will return only true if all the matrix is zero or false if one element is different than zero.
You can construct this behavior by broadcasting the inequality operator.
julia> x
5×5 Array{Int64,2}:
0 -4107730642120626124 6654664427866713002 7518855061140735034 8818106399735122346
8091546149111269981 4315717857697687985 0 -5798218902015720994 1300970799075893685
-7301322994135835673 -2297242472677021645 -4021288767260950802 7892625078388936975 -1629449791447953082
1060255079487701867 -5212584518144622345 7329251290490888722 1375278257655605061 -4361465961064184585
-469090114465458856 6912712453842322323 -1577327221996471827 -5606008086331742040 1641289265656709209
julia> !=(0).(x)
5×5 BitArray{2}:
0 1 1 1 1
1 1 0 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
The result is a BitArray, the canonical representation of a matrix with boolean values.

How to create an error function comparing two matrices?

I have two matrices in MATLAB. Each one is filled with 1 and 0 at different positions. I want to compare each element:
If there is a 1 match, I want it to record as True Positive.
If there is a 0 match, I want it to record as True Negative.
If one says 1 and the other says 0, I want to record as False Positive.
If one says 0 and the other says 1, I want to record as False Negative.
I tried just comparing the two matrices:
idx = A == B
But, that gives me a simple match, not telling me when there is a True Positive or Negative, etc.
Is there any specific function I could use, or any alternative?
You could just add the matrices in a prescribed way....
a = [1 0 1 0
1 1 0 0
0 0 1 1];
b = [1 0 0 0
0 0 0 1
0 0 1 0];
C = a + 2*b;
% For pairs [a,b] we expect
% [0,0]: C = 0, true negative
% [1,0]: C = 1, false positive
% [0,1]: C = 2, false negative
% [1,1]: C = 3, true positive
% C =
% [ 3 0 1 0
% 1 1 0 2
% 0 0 3 1 ]
If you have the Statistics and Machine Learning toolbox and you only want a summary, you might just need the function confusionmat.
From the docs:
C = confusionmat(group,grouphat) returns the confusion matrix C determined by the known and predicted groups in group and grouphat. [...]. C is a square matrix with size equal to the total number of distinct elements in group and grouphat. C(i,j) is a count of observations known to be in group i but predicted to be in group j.
For example:
a = [1 0 1 0
1 1 0 0
0 0 1 1];
b = [1 0 0 0
0 0 0 1
0 0 1 0];
C = confusionmat( a(:), b(:) );
% C =
% [ 5 1
% 4 2]
% So for each pair [a,b], we have 5*[0,0], 2*[1,1], 4*[1,0], 1*[0,1]
A similar function for those with the Neural Network Toolbox instead would be confusion.
You could just use bitwise operators to produce the four different values:
bitor(bitshift(uint8(b),1),uint8(a))
Produces an array with
0 : True Negative
1 : False Negative (a is true but b is false)
2 : False Positive (a is false but b is true)
3 : True Positive
One naive approach would be four comparisons, case by case:
% Set up some artificial data
ground_truth = randi(2, 5) - 1
compare = randi(2, 5) - 1
% Determine true positives, false positives, etc.
tp = ground_truth & compare
fp = ~ground_truth & compare
tn = ~ground_truth & ~compare
fn = ground_truth & ~compare
Output:
ground_truth =
1 0 1 0 0
0 1 1 0 1
1 1 0 1 0
0 1 0 1 1
0 0 0 1 0
compare =
0 1 1 0 1
0 1 1 1 0
1 1 0 0 1
1 1 1 0 0
1 1 1 1 1
tp =
0 0 1 0 0
0 1 1 0 0
1 1 0 0 0
0 1 0 0 0
0 0 0 1 0
fp =
0 1 0 0 1
0 0 0 1 0
0 0 0 0 1
1 0 1 0 0
1 1 1 0 1
tn =
0 0 0 1 0
1 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
fn =
1 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1
0 0 0 0 0
That works, because 0 and 1 (or any positive value) are alternative representations for true and false.
To keep your main code clean, set up a separate function, say my_stats.m
function [tp, fp, tn, fn] = my_stats(ground_truth, compare)
% Determine true positives, false positives, etc.
tp = ground_truth & compare;
fp = ~ground_truth & compare;
tn = ~ground_truth & ~compare;
fn = ground_truth & ~compare;
end
and call it in your main code:
% Set up some artificial data
ground_truth = randi(2, 5) - 1
compare = randi(2, 5) - 1
[tp, fp, tn, fn] = my_stats(ground_truth, compare)
Hope that helps!
I found that I can use the find method and set two conditions, then just find the numbers of the element in each variable
TruePositive = length(find(A==B & A==1))
TrueNegative = length(find(A==B & A==0))
FalsePositive = length(find(A~=B & A==1))
FalseNegative = length(find(A~=B & A==0))
The confusionmatrix() method suggested by #Wolfie is also really neat, especially if you use the confusionchart() which provides a nice visualisation.

Acces 3D Matrix with 2D index and 1D vector

I have a 3D matrix A (size m*n*k) where m=latitude, n*longitude and k=time.
I want only specific values from first and second dimension, specified by a logical matrix B (size m*n), and I want only the timesteps specified by vector C (size k).
In the end this should become a 2D matrix D, since the first two dimesions will colapse to one.
What is the most easy approach to do this?
And also is it possible to combine logical with linear indizes here? For example B is logical and C is linear?
Sample code with rand:
A=rand(10,10,10);
B=randi([0 1], 10,10);
C=randi([0 1], 10,1);
D=A(B,C) %This would be my approach which doesnt work. The size of D should be sum(B)*sum(c)
Another example without rand:
A=reshape([1:27],3,3,3);
B=logical([1,0,0;1,0,0;0,0,0]);
C=(1,3); %get data from timestep 1 and 5
D=A(B,C);%What I want to do, but doesnÄt work that way
D=[1,19;2,20];%Result should look like this! First dimension is now all data from dimesion 1 and 2. New dimesion 2 is now the time.
A = rand(4,4,4);
B = randi([0 1], 4,4)
B =
1 1 0 1
1 0 1 1
0 0 1 0
1 0 1 1
>> C = randi([0 1],1,1,4);
>> C(:)
ans =
0
1
1
0
Then use bsxfun or implicit expansion expansion whith .* if newer Matlab version to generate a matrix of logical for you given coordinates.
>> idx = logical(bsxfun(#times,B,C))
idx(:,:,1) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
idx(:,:,2) =
1 1 0 1
1 0 1 1
0 0 1 0
1 0 1 1
idx(:,:,3) =
1 1 0 1
1 0 1 1
0 0 1 0
1 0 1 1
idx(:,:,4) =
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Then your output is D = A(idx). However, note that this D is now an Nx1 array. Where N is number of true elements is B times number of true elements in C. 10x True in B and 2x True in C:
>> size(D)
ans =
20 1
An easy way to do it is to first reshape A into an m*n-by-k matrix, then do your indexing:
result = reshape(A, [], size(A, 3));
result = result(B, C);
In this case C can be either a logical vector or vector of indices.

MATLAB - Find contour of a binary bit map?

I have a 10x10 binary bit map as follows. I am looking for an efficient way of finding its contour in MATLAB. (I have tried letting every value "look around" its neighbors' values and decide, but it is too inefficient. I expect the algorithm to scale up.)
false false false false false false false false false false
false false true true true true true true false false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false false true true true true true true false false
false false false false false false false false false false
Let's assume each boolean value resembles a square, and the left-bottom one sits over x: 0-1; y: 0-1. The output should be the points that form the boundary. You may assume the inner true block is alway convex.
This is dead simple. Use the bwperim command in MATLAB, assuming you have the image processing toolbox.
You can call the function like so:
out = bwperim(A); %//or
out = bwperim(A,conn);
The first way assumes that the pixel connectivity is a 4-pixel neighbourhood. This will only look at the north, south, east and west directions.
If you specify an additional parameter called conn which is a single number, you can override this behaviour and specify the kind of behaviour you want when looking at neighbouring pixels. For example, if conn=8, you would look at 8-pixel neighbourhoods for 2D (so N, NE, E, SE, S, SW, W, NW), or you can go into 3D if you have a 3D binary image... but for now, I'm assuming it's just 2D. For the best accuracy, use 8.
As such, we have:
A = [false false false false false false false false false false
false false true true true true true true false false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false true true true true true true true true false
false false true true true true true true false false
false false false false false false false false false false];
out = bwperim(A,8);
And it looks like:
out =
0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 1 0 0
0 1 1 0 0 0 0 1 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0 1 0
0 1 1 0 0 0 0 1 1 0
0 0 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 0 0 0
MATLAB outputs 1 for true and 0 for false.
As a bonus, this is what the shapes look like side by side:
Edit from the comments
Going with your comments, you wish to find the set of points that make the perimeter. As such, you can simply use the find command to do that for you.
[X,Y] = find(out == 1);
coords = [X Y];
What the find command does is that it searches your array and finds locations in the array that match the Boolean expression given in the parameter of find. In this case, we wish to find all co-ordinates that have a pixel in out equal to 1, and out is our perimeter image. As such, this effectively finds all pixels that are perimeter pixels.
We thus get:
coords =
3 2
4 2
5 2
6 2
7 2
8 2
2 3
3 3
8 3
9 3
2 4
9 4
2 5
9 5
2 6
9 6
2 7
9 7
2 8
3 8
8 8
9 8
3 9
4 9
5 9
6 9
7 9
8 9
X are the row co-ordinates, while Y are the column co-ordinates. I've placed X and Y into a single 2D array for better presentation, but you can take the X and Y variables by themselves for further processing.
Hope this helps!
Here's another option:
B = bwboundaries(A)
this will get the x-y coordinates of any boundary. see more info here...
Another option with image processing toolbox:
B = A - imerode(A,SE);
where SE is one of the kernels:
0 1 0 1 1 1
1 1 1 1 1 1
0 1 0 1 1 1
depending on which connectivity you want to use: first one for 8-connectivity, second one for 4-connectivity. For the difference between the two, recall that 8-connectivity allows diagonal neighbours.
With the image B, you can find all the points of the perimeters with the same technique displayed in another answer:
[Xp,Yp] = find(B);

howto find out if a vector contains only one 1 and other are 0? or how to check if every entry is the same?

howto find out if a vector contains only one 1 and other are 0? or how to check if every entry is the same?
e.g. i need to check if a vector contains zeros except only one 1 like:
(0 0 0 0 1 0 0 0 0) -> true
(0 0 0 0 0 0 0 0 1) -> true
(0 0 0 0 2 0 0 0 0) -> false
(0 0 1 0 1 0 0 0 0) -> false
You can use logical indexing, assuming your vector is v: numel(v(v==1)) returns the number of elements equal to 1 in your vector.
In the same way, if you want to check if every value is the same you can use: numel(unique(v)) which returns the number of unique entries of v.
A slightly different solution:
v = [0 0 0 0 1 0 0 0 0];
TF = sum(v==1)==1 %# returns TRUE
This is especially useful if you want to apply it to all rows of a matrix:
M = [
0 0 0 0 1 0 0 0 0 ;
0 0 0 0 0 0 0 0 1 ;
0 0 0 0 2 0 0 0 0 ;
0 0 1 0 1 0 0 0 0
];
TF = sum(M==1,2)==1
The result:
>> TF
TF =
1
1
0
0
The check for only zeroes could be achieved by extracting all unique elements from your variable:
u = unique (v)
You can then compare the result to zero and voila.
To check for a non-zero element, use the find function. If it finds only one index and that entry is one, your desired result is true. Otherwise it's false.
function bool = oneone(vector)
num = find(vector);
bool = isscalar(num) && vector(num)==1;
end
For all same entries, the diff function calculates the difference of subsequent elements. If any of the results are non-zero, your desired result is false.
function bool = allsame(vector)
d = diff(vector);
bool = ~any(d);
end