Improving speed in construction of a matrix

Hi.
Im currently implementing a code, where part of the code constructs a matrix several million times. Currently, the construction of this matrix takes much more time, than the actual computing invovled with the matrix. Its a rather simple matrix, but takes a third of the total runtime. I want to construct it faster, but cant seem to bring the time down. Ive tried two approaches.
One implementation:
B = zeros(9,9);
B(1:3,1) = A(1,:);
B(4:6,2) = A(1,:);
B(7:9,3) = A(1,:);
B(1:3,4) = A(2,:);
B(4:6,5) = A(2,:);
B(7:9,6) = A(2,:);
B(1:3,7) = A(3,:);
B(4:6,8) = A(3,:);
B(7:9,9) = A(3,:);
And the other implementation:
Z = zeros(3,1);
B =[A(:,1) Z Z A(:,2) Z Z A(:,3) Z Z
Z A(:,1) Z Z A(:,2) Z Z A(:,3) Z
Z Z A(:,1) Z Z A(:,2) Z Z A(:,3)];
Both giving a inaduqate execution time. Is there a faster way to construct this type of matrix?

6 Kommentare

Torsten
Torsten am 16 Jan. 2024
Bearbeitet: Torsten am 16 Jan. 2024
How long is "inadequate" ?
A = rand(3,3);
B = zeros(9,9);
Z = zeros(3,1);
tic
for i = 1:100000
B(1:3,1) = A(1,:);
B(4:6,2) = A(1,:);
B(7:9,3) = A(1,:);
B(1:3,4) = A(2,:);
B(4:6,5) = A(2,:);
B(7:9,6) = A(2,:);
B(1:3,7) = A(3,:);
B(4:6,8) = A(3,:);
B(7:9,9) = A(3,:);
end
toc
Elapsed time is 0.979855 seconds.
tic
for i = 1:100000
B =[A(:,1) Z Z A(:,2) Z Z A(:,3) Z Z
Z A(:,1) Z Z A(:,2) Z Z A(:,3) Z
Z Z A(:,1) Z Z A(:,2) Z Z A(:,3)];
end
toc
Elapsed time is 0.620697 seconds.
Z = zeros(3,1);
A1 = A(:,1); A2 = A(:,2); A3 = A(:,3);
B =[A1 Z Z A2 Z Z A3 Z Z
Z A1 Z Z A2 Z Z A3 Z
Z Z A1 Z Z A2 Z Z A3];
Your two implementations give different results:
A=reshape(1:9,3,3)
A = 3×3
1 4 7 2 5 8 3 6 9
B = zeros(9,9);
B(1:3,1) = A(1,:);
B(4:6,2) = A(1,:);
B(7:9,3) = A(1,:);
B(1:3,4) = A(2,:);
B(4:6,5) = A(2,:);
B(7:9,6) = A(2,:);
B(1:3,7) = A(3,:);
B(4:6,8) = A(3,:);
B(7:9,9) = A(3,:)
B = 9×9
1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 0 0 9 0 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 0 0 9 0 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 0 0 9
Z = zeros(3,1);
B =[A(:,1) Z Z A(:,2) Z Z A(:,3) Z Z
Z A(:,1) Z Z A(:,2) Z Z A(:,3) Z
Z Z A(:,1) Z Z A(:,2) Z Z A(:,3)]
B = 9×9
1 0 0 4 0 0 7 0 0 2 0 0 5 0 0 8 0 0 3 0 0 6 0 0 9 0 0 0 1 0 0 4 0 0 7 0 0 2 0 0 5 0 0 8 0 0 3 0 0 6 0 0 9 0 0 0 1 0 0 4 0 0 7 0 0 2 0 0 5 0 0 8 0 0 3 0 0 6 0 0 9
cTroels
cTroels am 16 Jan. 2024
The function is called 1-10 million times, dependent on the problem, taking 40 seconds of 120 seconds total runtime.
Matt J
Matt J am 16 Jan. 2024
Bearbeitet: Matt J am 16 Jan. 2024
I think it unlikely you are going to be able to reliably optimize such an infinitessimal task in MCode. Different computers will give you different relative performance. You need to try to reorganize your computation in large vectorized batches.
cTroels
cTroels am 16 Jan. 2024
I was able to bring the execution time of the function down to 4s by just writing out the whole matrix explicitly. Should have done that initially i guess. But thanks for the suggestions! Appreciate the inputs.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Benjamin Thompson
Benjamin Thompson am 20 Jan. 2024

0 Stimmen

Not sure where your data for A is coming from, or where the output of B is going to. if each A -> B mapping is independent try parfor if you have a multicore processor or using a gpuArray.

Weitere Antworten (0)

Produkte

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by