Creating a matrix of blank spaces.

If I wanted to create a matrix of blanks spaces I would use the following command:
A = [' ', ' '; ' ', ' ';];
but what if I do not know the length or width of the matrix? If the matrix was for ints or double types it would be easy:
A = zeros(column_number, row_number);
Is there a command that does the same thing as the zeros command but for characters?

 Respuesta aceptada

Sean de Wolski
Sean de Wolski el 20 de En. de 2011

10 votos

You could use repmat:
repmat(' ',[3 3])

8 comentarios

Paulo Silva
Paulo Silva el 20 de En. de 2011
char(double(' ')*ones(3,3))
Ned Gulley
Ned Gulley el 20 de En. de 2011
Paulo, that's a good answer. You should add it as another answer (rather than as a comment) so that it can earn votes and perhaps get it accepted by the question's author.
Paulo Silva
Paulo Silva el 20 de En. de 2011
Hi Ned, my answer isn't that good, Sean code is faster and simpler than mine, it was just to give another alternative :)
Walter Roberson
Walter Roberson el 20 de En. de 2011
Or mildly faster but more obscure than repmat would be
blank = '';
blank(ones(column_number,row_number))
This is what repmat will end up doing internally.
Walter Roberson
Walter Roberson el 20 de En. de 2011
Paulo, the double() is not needed:
char(' ' * ones(column_number,row_number))
Walter Roberson
Walter Roberson el 20 de En. de 2011
Also,
char(' ' + zeros(column_number,row_number))
Jan
Jan el 28 de Jun. de 2012
Accepted by JSimon
Patrik Ek
Patrik Ek el 23 de En. de 2014
Editada: Patrik Ek el 23 de En. de 2014
It is only so annoying that it is no constructor for char in matlab. This must be much slower than using an ordinary constructor.

Iniciar sesión para comentar.

Más respuestas (4)

Kenneth Eaton
Kenneth Eaton el 20 de En. de 2011

8 votos

If you are only making a 1-D matrix (i.e. a row or column vector) then the function BLANKS is the way to go:
blankStr = blanks(4); % Makes a string of 4 blanks
For a 2-D matrix, the REPMAT-based solution from Sean de is probably the simplest, but here's another variant using the functions RESHAPE and BLANKS:
blankMat = reshape(blanks(4),2,2); % Makes a 2-by-2 matrix of blanks
Petter
Petter el 20 de En. de 2011

1 voto

Yet another way:
char(' '*char(ones(m,n)))
Hy
Hy el 20 de En. de 2011

1 voto

There is not a command for characters that is analogous to zeros. Common commands for initializing matrices of size M x N (where sz = M*N) include
The function blanks will produce a row vector. It is convenient because it avoids including a literal space character in your code, but specifying your choice of whitespace character and considering Locale Settings may produce clearer code.
Please note that because MATLAB uses column-major order [Wikipedia], I think that your example syntax should read
A = zeros(number_of_rows, number_of_columns);
The solutions by Sean and Kenneth can be restated using this syntax as:
sz = [number_of_rows, number_of_columns];
A = reshape(blanks(prod(sz)), sz);
B = repmat(' ', sz);
display(isequal(A,B));
All of these solutions work equally well to produce arrays of dimension 1, 2, ... n.

2 comentarios

Walter Roberson
Walter Roberson el 20 de En. de 2011
I would only consider blanks() to be cleaner if it was documented as possibly using some Locale-specific horizontal whitespace, or if blanks() had options to produce Unicode whitespace characters of width em-dash or en-dash or similar use (preferably font-size relative.) But as long as blanks is confined to producing basic spaces, in my opinion using it instead of a simple literal single white space just confuses the coding.
Hy
Hy el 21 de En. de 2011
Updated answer to reflect Walter's correction.

Iniciar sesión para comentar.

Malcolm Lidierth
Malcolm Lidierth el 21 de En. de 2011

0 votos

Why is there no direct equivalent to zeros for chars? In most languages, strings are zero terminated so x=char(zeros(1,10)) may be a 10 element row vector in MATLAB's internal tables but will be perceived as a null string in most languages. Writing to a pre-allocated, but empty, char vector from a DLL or mex-file will usually crash MATLAB with a segmentation violation regardless of what numel returned in MATLAB before the call. Filling with non-zeros instead, e.g. blanks, avoids that. With short strings, my habit is to use '0123...'.

Categorías

Más información sobre Entering Commands en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 20 de En. de 2011

Editada:

el 23 de En. de 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by