# Improving speed in construction of a matrix

2 views (last 30 days)

Show older comments

cTroels
on 16 Jan 2024

Answered: Benjamin Thompson
on 20 Jan 2024

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 Comments

### Accepted Answer

Benjamin Thompson
on 20 Jan 2024

##### 0 Comments

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!