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

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.

Related

Combinations of zeros and ones of vector size n [duplicate]

I have an algorith that the number of possibles combinations of 0 and 1, can reach the number 2^39. Let's say i have n=2 situations, or n1=2^2=4 combinations of 0 and 1: 00,01,10,11.From that i can create an array a=zeros(n,n1) and fill the columns with the possible combinations? That means first column has 00,second 01,third 10,last 11.I want this to be dynamic that means that n can be 1,2,3...,39, show the array will be a=zeros(n,2^n).Thanks for any response!
Just for general understanding: why do you think you need an array of all combinations of all integers from 0 to 2³⁹? That array would consume 39×2³⁹/1000⁴ ≈ 21TB of RAM...last time I checked, only the world's most advanced supercomputers have such resources, and most people working with those machines consider generating arrays like this quite wasteful...
Anyway, for completeness, for any N, this is the simplest solution:
P = dec2bin(0:(2^N)-1)-'0'
But, a little piece of advice: dec2bin outputs character arrays. If you want numerical arrays, you can subtract the character '0', however, that gives you an array of doubles according to the rules of MATLAB:
>> P = dec2bin(0:(2^3)-1)-'0';
>> whos P
Name Size Bytes Class Attributes
P 8x3 192 double
If you want to minimize your memory consumption, generate a logical array instead:
>> P = dec2bin(0:(2^3)-1)=='1';
>> whos P
Name Size Bytes Class Attributes
P 8x3 24 logical
If you want to also speed up the execution, use the standard algorithm directly:
%// if you like cryptic one-liners
B1 = rem(floor((0:pow2(N)-1).' * pow2(1-N:0)), 2) == 1;
%// If you like readability
B = false(N,pow2(N));
V = 0:pow2(N)-1;
for ii = 1:N
B(ii,:) = rem(V,2)==1;
V = (V-B(ii,:))/2;
end
That last one (the loop) is fastest of all solutions for any N (at least on R2010b and R2013a), and it has the smallest peak memory (only 1/Nth of the cryptic one-liner).
So I'd go for that one :)
But, that's just me.
Using ndgrid with a comma-separated list as output (see also here):
[c{1:N}] = ndgrid(logical([0 1]));
c = cat(N+1,c{N:-1:1});
c = reshape(c,[],N);
Example: N=4 gives
c =
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1

How to compare 2 matrices and keep greatest values in Matlab

I am using Matlab and have 2 matrices:
A =
0 0 1
1 0 1
1 1 0
and
B =
0 0 1.1
1.0 0 0.8
1.2 0.8 0
My goal is to compare the 2 matrices and get the highest values of each i,j element and store such results into a third matrix C.
I am able to achieve such result by applying a for loop that checks every single element and then stores it into the matrix but i would like to have a more efficient and elegant way to do so. Can you suggest me some better way?
The result should be like:
C =
0 0 1.1
1 0 1
1.2 1 0
C = max(A,B)
Source on documentation

Counting islands in a matrix (preferably in IDL)

How can I count the number of values that are contiguous in an matrix? For example, if
A= [ 1 0 1 0 0 0 0 \
1 1 1 0 0 0 1\
1 1 0 1 1 1 1]
is a 7 by 3 matrix then the result should indicate that there are 12 contiguous values that are "1" (highlighted in bold), that there are 8 contiguous 0 values (highlighted in italics) and one lone 0 value. Code in IDL is preferred but MATLAB would also be helpful.
This is what you can do in MATLAB using the bwconncomp function. This is a function in the Image Processing Toolbox. I don't know a whole lot about IDL, it might have a similar function.
bwconncomp returns a struct with some information, one of the fields is PixelIdxList, which is a cell array with one element per connected component. Each of these elements is a vector with the indices to one of the array elements in the connected component. For the case of the 1 elements in your example, this cell array will have one vector with 12 values. For the case of the 0 elements, it will have two vectors, one with 1 value and one with 8:
>> A = [ 1 0 1 0 0 0 0 ; 1 1 1 0 0 0 1 ; 1 1 0 1 1 1 1 ];
>> CC = bwconncomp(A==1, 8);
>> cellfun(#numel, CC.PixelIdxList)
ans =
12
>> CC = bwconncomp(A==0, 8);
>> cellfun(#numel, CC.PixelIdxList)
ans =
1 8
The bwconncomp takes 4 or 8 as the second argument. This specifies what are considered connected elements (contiguous values, neighbors). 4 means only the 4 elements N,S,E,W are connected; 8 means also diagonal connections exist (8 neighbors).

MatLab - Cellfun where func = strcmp Find where str changes in a cell array

I have a cell array of strings, I want to detect the num of times the string changes and get the indxs for the changes. Given Matlab's cellfun function, I am trying to use it instead of looping. Here is all the code. I appreciate you time, feedback, and comments.
% Cell Array Example
names(1:10)={'OFF'};
names(11:15)={'J1 - 1'};
names(16:22)={'J1 - 2'};
names(23:27)={'J2 - 1'};
names(28)={'Off'};
names=names';
% My cellfun code
cellfun(#(x,y) strcmp(x,y), names(1:2:end),names(2:2:end));
My expected result is a vector of length 27 (length(names)-1), where there are 4 zeros in the vector indicating the strcmp func found 4 cases where the comparison was not equal.
The actual result is a vector of length 14 and has only 2 zeros. I'd really appreciate an explanation, why this unexpected result is occurring.
Thank You
The answer provided by Matt correctly shows the issue with your code. However, you can use strcmp directly because it accepts two cell array of strings as input
>> strcmp(names(1:end-1), names(2:end))
ans =
Columns 1 through 14
1 1 1 1 1 1 1 1 1 0 1 1 1 1
Columns 15 through 27
0 1 1 1 1 1 1 0 1 1 1 1 0
You could transform the strings into numeric labels using unique, and then apply diff to detect changes:
[~, ~, u] = unique(names);
result = ~diff(u);
If I understand your question correctly, you should be comparing names(1:end-1) with names(2:end). That is, compare string 1 with string 2, compare string 2 with string 3, and so on. You are instead using a stride of 2, comparing string 1 with string 2, string 3 with string 4, and so on. You can fix this by changing your last line to:
cellfun(#(x,y) strcmp(x,y), names(1:end-1),names(2:end))
The result is then:
Columns 1 through 20:
1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1
Columns 21 through 27:
1 0 1 1 1 1 0

Matlab create vectorized sequence

I want to construct a function that accepts input n and gives the vector
[n n-1 n-2 ... n-n, n-1 n-2 ... n-n, ..., n-n]
//Example
input : n=3
output : [3 2 1 0 2 1 0 1 0 0]
I know how to do this using loops, but I'm looking for a clever way to do it in MATLAB
You can use repmat to repeat the matrix a few times, and then select only the triangular part by means of tril. Like this:
n=3;
x=repmat(n:-1:0,1,n+1);
result=x(tril(ones(n+1))>0)
Or in one line:
n=3;
getfield(repmat(n:-1:0,1,n+1),{reshape(tril(ones(n+1))>0,1,(n+1)^2)})
The result of this function is the desired output:
result =
3 2 1 0 2 1 0 1 0 0
Since you haven't gotten any answers, here's a way to do it:
N = 3;
x = repmat(N:-1:0,1,N+1)-cumsum(repmat([1 zeros(1,N)],1,N+1))+1
x = x(x>=0)
x =
3 2 1 0 2 1 0 1 0 0