I have so far been using
a = logical(zeros(10,10))
to create logical matrix.
Is there a way to create it directly?
One way of creating an array of logicals is to just enter a true or false value for each element. The true function returns logical one; the false function returns logical zero: x = [true, true, false, true, false];
L = logical( A ) converts A into an array of logical values. Any nonzero element of A is converted to logical 1 ( true ) and zeros are converted to logical 0 ( false ). Complex values and NaNs cannot be converted to logical values and result in a conversion error.
In MATLAB, you create a matrix by entering elements in each row as comma or space delimited numbers and using semicolons to mark the end of each row. In the same way, you can create a sub-matrix taking a sub-part of a matrix. In the same way, you can create a sub-matrix taking a sub-part of a matrix.
Use false directly:
a = false(10,10);
In Matlab, there are a few 'constants' that are actually matrix-generating commands. They are:
true(10); true(n,m,k,..); % for an all-true 10-by-10 or n-by-m-by-k-by-...matrix
false(10); false(n,m,k,..); % for an all-false 10-by-10 or n-by-m-by-k-by-...matrix
inf(10); inf(n,m,k,..); % for an all-inf 10-by-10 or n-by-m-by-k-by-...matrix
nan(10); nan(n,m,k,..); % for an all-nan 10-by-10 or n-by-m-by-k-by-...matrix
The 'constant' forms are basically just aliases:
a = inf; % alias for a = inf(1);
b = true; % alias for b = true(1);
etc. Also, here are some more things that cannot be repeated enough:
rand(10); rand(n,m,k,..); % for an all-random 10-by-10 or n-by-m-by-k-by-...matrix
zeros(10); zeros(n,m,k,..); % for an all-zero 10-by-10 or n-by-m-by-k-by-...matrix
zeros(n,m,k,...,'uint8'); % for an all-zero n-by-m-by-k-by-...matrix of type uint8
ones(10); zeros(n,m,k,..); % for an all-one 10-by-10 or n-by-m-by-k-by-...matrix
ones(n,m,k,...,'uint8'); % for an all-one n-by-m-by-k-by-...matrix of type uint8
eye(10); eye(n,m,'uint8'); % for an identity matrix of type uint8 of 10-by-10 or n-by-m
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With