MATLAB Answers

0

initialize a MxN matrix with the same number

Salvatore Mazzarino さんによって質問されました 2012 年 10 月 20 日
最新アクティビティ Matt J
さんによって 回答されました 2018 年 11 月 11 日
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 件のコメント

サインイン to comment.

タグ

8 件の回答

Login_Name
回答者: Login_Name
2012 年 10 月 20 日

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

  3 件のコメント

Jan
2012 年 10 月 20 日
This method can cause problems, if A has been defined before, e.g. by A = rand(9) or A = 'string'.
John BG
2016 年 9 月 28 日
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.
Walter Roberson
2016 年 9 月 29 日
? 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.

サインイン to comment.


Azzi Abdelmalek
回答者: Azzi Abdelmalek
2012 年 10 月 20 日
編集済み: Azzi Abdelmalek
2012 年 10 月 20 日

A=zeros(M,N)

  4 件のコメント

Azzi Abdelmalek
2012 年 10 月 20 日
A=ones(M,N)*yournumber
Azzi Abdelmalek
2012 年 10 月 20 日
or
A=repmat(12,M,N);
Azzi Abdelmalek
2012 年 10 月 20 日
Matt's init is the fastest

サインイン to comment.


Jan
回答者: Jan
2012 年 10 月 20 日

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 件のコメント

サインイン to comment.


Friedrich
回答者: Friedrich
2018 年 8 月 14 日
編集済み: Friedrich
2018 年 8 月 15 日

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 件のコメント

James Tursa
2018 年 8 月 14 日
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);
}

サインイン to comment.


Matt J
回答者: Matt J
2012 年 10 月 20 日

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

  0 件のコメント

サインイン to comment.


James Tursa
回答者: James Tursa
2012 年 10 月 20 日
編集済み: James Tursa
2012 年 10 月 20 日

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 件のコメント

サインイン to comment.


回答者: MathWorks Support Team 2018 年 11 月 9 日

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 件のコメント

サインイン to comment.


Matt J
回答者: Matt J
2018 年 11 月 11 日

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

  0 件のコメント

サインイン to comment.



Translated by