How to insert different size matrices into each row of a zero matrix

3 views (last 30 days)
I am using a for loop to solve multiple itterations of an equation. for each output, i wan it to store in a new row of a zero matrix, while keeping the output from the previous itteration as shown below
M = [ x1 0 0 0 ;
x1 x2 0 0 ;
x1 x2 x3 0;
x1 x2 x3 x4]

Answers (3)

KSSV
KSSV on 10 Apr 2023
M = zeros(4) ;
for i = 1:4
M(i,1:i) = rand(1,i) ;
end
M
M = 4×4
0.3674 0 0 0 0.1426 0.9721 0 0 0.8203 0.1518 0.5443 0 0.9805 0.8734 0.5388 0.7684

Walter Roberson
Walter Roberson on 10 Apr 2023
newvalues = as appropriate
M(end+1, 1:length(newvalues)) = newvalues;

VBBV
VBBV on 10 Apr 2023
syms x [1 10]
n = 10;
for k = 1:n
M(k,1:k) = x(1:k);
end
M
M = 

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!