How to only fill one line of a matrix with a given formula

4 views (last 30 days)
I am trying to fill a 3*n matrix, with the first row being ones, the second row being 20 0 repeating. and the third row being 30 0 0 repeating.
Example:
For matrix n = 10, it should look like this
1 1 1 1 1 1 1 1 1 1
20 0 20 0 20 0 20 0 20 0
30 0 0 30 0 0 30 0 0 30
function A = specialMatrix(n)
A = zeros(3,n);
A(1,:) = 1
A(11:2:end) = 20
A(21:3:end) = 30
end
However, this does not fill up the matrix in the desired way.
How can I format my coding to fill up the matrix correctly?

Answers (1)

Voss
Voss on 22 Oct 2023
function A = specialMatrix(n)
A = zeros(3,n);
A(1,:) = 1;
A(2,1:2:end) = 20;
A(3,1:3:end) = 30;
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!