Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create logical matrix directly in matlab

Tags:

matrix

matlab

I have so far been using

 a = logical(zeros(10,10))

to create logical matrix.

Is there a way to create it directly?

like image 432
nos Avatar asked Nov 06 '12 03:11

nos


People also ask

How do you create a logical array in MATLAB?

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];

How do I create a logical function in MATLAB?

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.

How do we generate the matrix in MATLAB?

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.


2 Answers

Use false directly:

a = false(10,10);
like image 111
tmpearce Avatar answered Oct 11 '22 14:10

tmpearce


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
like image 8
Rody Oldenhuis Avatar answered Oct 11 '22 12:10

Rody Oldenhuis