# initialize a MxN matrix with the same number

1,864 ビュー (過去 30 日間)
Salvatore Mazzarino 2012 年 10 月 20 日

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]

サインインしてコメントする。

### 回答 (9 件)

Matt Fig 2012 年 10 月 20 日
Another:
% Make a 3-by-8 matrix of 9s:
A(1:3,1:8) = 9
##### 3 件のコメント表示非表示 2 件の古いコメント
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.

サインインしてコメントする。

Friedrich 2018 年 8 月 14 日

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.
##### 5 件のコメント表示非表示 4 件の古いコメント
Rik 2020 年 6 月 18 日
I also added your suggestion with randi and reposted it as an answer to avoid this being burried.

サインインしてコメントする。

Azzi Abdelmalek 2012 年 10 月 20 日

A=zeros(M,N)
##### 4 件のコメント表示非表示 3 件の古いコメント
Azzi Abdelmalek 2012 年 10 月 20 日
Matt's init is the fastest

サインインしてコメントする。

Rik 2020 年 6 月 18 日

Inspired by the comparative speed test in the answer by Friedrich, I extended his code to have more robust testing that could be performed on multiple different releases. Timings below are normalized to 10*ones(M,N), and Octave and ML6.5 have fewer elements to prevent max array size errors. See the attached file for all details.
(failed options are removed from this post, but are displayed by the function) (you could make the function more fancy, but I didn't feel like spending time on that. I might update this function at some point)
Matlab 6.5:
A=ones(M,N)*10; = 0.0410s (normalized time = 0.99)
A=zeros(M,N)+10; = 0.0400s (normalized time = 1.01)
A=repmat(10,[M,N]) = 0.0460s (normalized time = 1.14)
a=10;A=a(ones(M, N)); = 0.2770s (normalized time = 6.84)
R2011a:
A=repmat(10,[M,N]) = 1.8918s (normalized time = 0.98)
A=ones(M,N)*10; = 1.8975s (normalized time = 0.99)
A=uninit(M,N); A(:)=10; = 1.9129s (normalized time = 1.00)
A=zeros(M,N)+10; = 2.0259s (normalized time = 1.05)
A=mxFastZeros(0,M,N)+10; = 3.7976s (normalized time = 1.94)
a=10;A=a(ones(M, N)); = 8.4049s (normalized time = 4.36)
A=randi([10,10], M,N); = 12.0829s (normalized time = 6.20)
R2015a:
A=repmat(10,[M,N]) = 0.6856s (normalized time = 0.39)
A=repelem(10, M, N); = 0.7803s (normalized time = 0.44)
A=mxFastZeros(0,M,N)+10; = 1.7123s (normalized time = 0.97)
A=uninit(M,N); A(:)=10; = 1.7477s (normalized time = 0.99)
A=ones(M,N)*10; = 1.7598s (normalized time = 0.99)
A=zeros(M,N)+10; = 1.8429s (normalized time = 1.04)
a=10;A=a(ones(M, N)); = 7.6893s (normalized time = 4.35)
A=randi([10,10], M,N); = 7.7680s (normalized time = 4.40)
R2020a:
A=mxFastZeros(0,M,N)+10; = 0.6672s (normalized time = 0.31)
A=zeros(M,N)+10; = 0.6879s (normalized time = 0.31)
A=repmat(10,[M,N]) = 0.7013s (normalized time = 0.32)
A=repelem(10, M, N); = 0.7761s (normalized time = 0.36)
A=uninit(M,N); A(:)=10; = 1.7886s (normalized time = 0.83)
A=ones(M,N)*10; = 2.1304s (normalized time = 0.98)
A=randi([10,10], M,N); = 6.9900s (normalized time = 3.21)
a=10;A=a(ones(M, N)); = 7.2315s (normalized time = 3.32)
Octave 5.2.0
A=repmat(10,[M,N]) = 0.0264s (normalized time = 0.43)
A=repelem(10, M, N); = 0.0366s (normalized time = 0.59)
A=zeros(M,N)+10; = 0.0583s (normalized time = 0.94)
A=uninit(M,N); A(:)=10; = 0.0628s (normalized time = 1.00)
A=ones(M,N)*10; = 0.0637s (normalized time = 1.03)
a=10;A=a(ones(M, N)); = 0.0945s (normalized time = 1.53)
A=randi([10,10], M,N); = 0.4947s (normalized time = 8.00)
##### 3 件のコメント表示非表示 2 件の古いコメント
James Tursa 2020 年 6 月 18 日
Looks like they have exposed the true interfaces to these unofficial functions. Just use them. E.g.,
mxArray *mxFastZeros(int cmplx_flag, int m, int n);
mxArray *mxCreateSharedDataCopy(const mxArray *mx);

サインインしてコメントする。

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 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

Matt J 2012 年 10 月 20 日
A=zeros(M,N);
A(:)=some_number;
##### 0 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

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 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

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 件のコメント表示非表示 -1 件の古いコメント

サインインしてコメントする。

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

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!

Translated by