MATLAB Answers

0

initialize a MxN matrix with the same number

Asked by Salvatore Mazzarino on 20 Oct 2012
Latest activity Answered by Matt J
on 11 Nov 2018
I would initialize a M x N matrix with the same number. Which could be the best way in terms of speed?
Es.
[2 2;
2 2
2 2]

  0 Comments

Sign in to comment.

Tags

8 Answers

Answer by Login_Name
on 20 Oct 2012

Another:
% Make a 3-by-8 matrix of 9s:
A(1:3,1:8) = 9

  3 Comments

This method can cause problems, if A has been defined before, e.g. by A = rand(9) or A = 'string'.
if it has been defined before ..
if it coincides with the name of a function ..
if you start looking backward there is not way to move forward.
Matt gave the right answer, get on with it, or prove it wrong.
? You are arguing with a 4 year old posting ?
Jan did give a counter example:
A = rand(9);
A(1:3, 1:8) = 9;
A
A =
9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 0.9651
9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 0.6406
9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 9.0000 0.7577
0.5009 0.5300 0.3514 0.0230 0.6206 0.5925 0.8718 0.5488 0.7359
0.8410 0.9315 0.2206 0.2301 0.4299 0.1449 0.7987 0.3064 0.6590
0.9057 0.9739 0.3609 0.8522 0.6744 0.5350 0.7201 0.2121 0.9933
0.2481 0.8476 0.1054 0.9497 0.9710 0.2542 0.0973 0.6881 0.8679
0.1017 0.7075 0.1900 0.1831 0.3252 0.8435 0.3257 0.7090 0.4237
0.5273 0.9981 0.1697 0.2163 0.9954 0.9812 0.1355 0.4648 0.6465
Part of the array was set as required but the rest was left alone, which does not meet the specifications.

Sign in to comment.


Answer by Azzi Abdelmalek
on 20 Oct 2012
Edited by Azzi Abdelmalek
on 20 Oct 2012

A=zeros(M,N)

  4 Comments

Show 1 older comment
A=ones(M,N)*yournumber
Matt's init is the fastest

Sign in to comment.


Answer by Jan
on 20 Oct 2012

To avoid troubles with earlier definitions, I prefer:
A = repmat(12, M, N);
The overhead for calling the M-file repmat can be omitted:
a = 12;
A = a(ones(M, N));

  0 Comments

Sign in to comment.


Answer by Matt J
on 20 Oct 2012

A=zeros(M,N);
A(:)=some_number;

  0 Comments

Sign in to comment.


Answer by James Tursa
on 20 Oct 2012
Edited by James Tursa
on 20 Oct 2012

Another method if matrix A is not already allocated:
A = uninit(M,N);
A(:) = some_number;
UNINIT can be found here:
If the matrix A is pre-existing, then of course skip the allocation step and just fill the values ala the 2nd line above.
SIDE NOTE: On later version of MATLAB it seems the parser is smart enough to recognize the value*ones(m,n) formulation and not actually do the multiply. At least that is my conclusion based on speed tests.

  0 Comments

Sign in to comment.


Answer by Friedrich on 14 Aug 2018
Edited by Friedrich on 15 Aug 2018

I know this is old but I could not let it go. I found
A=zeros(M,N)+10;
to be the fastest. At least on my computer. Heres my code for testing and the results in Matlab 2017b
% produces 6.4GB of data
M = 80e6;
N = 10;
clear A
tic;
A=ones(M,N)*10;
disp(['A=ones(M,N)*10; = ' num2str(toc) 's']);
clear A
tic;
A=uninit(M,N);
A(:) = 10;
disp(['A=uninit(M,N); A(:)=10; = ' num2str(toc) 's']);
clear A
tic;
A=repmat(10,[M,N]);
disp(['A=repmat(10,[M,N]); = ' num2str(toc) 's']);
clear A
tic;
A = mxFastZeros(0,M,N)+10;
disp(['A=mxFastZeros(0,M,N)+10; = ' num2str(toc) 's']);
clear A
tic;
A=zeros(M,N)+10;
disp(['A=zeros(M,N)+10; = ' num2str(toc) 's']);
clear A
tic;
a = 12;
A = a(ones(M, N));
disp(['a=10;A=a(ones(M, N)); = ' num2str(toc) 's']);
clear A
Results
A=ones(M,N)*10; = 3.312s
A=uninit(M,N); A(:)=10; = 2.508s
A=repmat(10,[M,N]); = 2.1169s
A=mxFastZeros(0,M,N)+10; = 1.8326s
A=zeros(M,N)+10; = 1.8487s
a=10;A=a(ones(M, N)); = 25.0576s
Edit: Thank you James for the hint on mxFastZeros. I included that in the benchmark.

  1 Comment

Here's another one you can try using the undocumented API function mxFastZeros:
A = mxFastZeros(0,M,N)+10;
The behavior of this seems highly version dependent. In some versions it seems to run fast at first and then maybe exhaust the supply of pre-0'ed data and the timings jump up significantly. But just guessing here.
The mex code:
/* mxFastZeros.c generates a zero 2D double matrix
Syntax: z = mxFastZeros( ComplexFlag, M, N )
Where:
ComplexFlag = 0 (real) or 1 (complex)
M = row size
N = column size
Programmer: James Tursa
*/
#include "mex.h"
#undef mxFastZeros
#undef mxCreateSharedDataCopy
mxArray *mxFastZeros(mxComplexity ComplexFlag, mwSize m, mwSize n);
mxArray *mxCreateSharedDataCopy(mxArray *mx);
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *mx;
mxComplexity ComplexFlag;
mwSize m, n;
if( nrhs != 3 ) {
mexErrMsgTxt("Syntax: mxFastZeros(ComplexFlag,M,N)");
}
if( nlhs > 1 ) {
mexErrMsgTxt("Too many outputs.");
}
ComplexFlag = mxGetScalar(prhs[0]);
m = mxGetScalar(prhs[1]);
n = mxGetScalar(prhs[2]);
mx = mxFastZeros(ComplexFlag,m,n);
plhs[0] = mxCreateSharedDataCopy(mx);
mxDestroyArray(mx);
}

Sign in to comment.


Answer by MathWorks Support Team on 9 Nov 2018

In general, the easiest ways to initialize a matrix with the same number are the following, which produce a 3-by-2 matrix whose elements are all 2:
A = 2*ones(3,2)
A = zeros(3,2) + 2
A = repmat(2,3,2)
The speed of these methods relative to each other can depend on your computing environment.

  0 Comments

Sign in to comment.


Answer by Matt J
on 11 Nov 2018

Here's a safe one-liner, but I don't know how fast it is.
A=randi([n,n], M,N);

  0 Comments

Sign in to comment.