How to make a 3x3 matrix

Hello every one; I want to make a 3x3 general matrix using three different function as shown in code. It is working but I want to know how to save the results in three different lines of same matrix. Also how to run this matrix in command window for some defined value of theta. Thanks
syms theta;
for x=1:3
beta=0:2*pi/3:4*pi/3;
if x==1
L1 =cos(theta+beta);
elseif x==2
L2=cos(theta+beta-2*pi/3);
else
L3=cos(theta+beta+2*pi/3);
end
end

 Accepted Answer

Jan
Jan on 4 Jun 2018
Edited: Jan on 4 Jun 2018
syms theta;
beta = 0:2*pi/3:4*pi/3;
L = [cos(theta+beta); ...
cos(theta+beta-2*pi/3); ...
cos(theta+beta+2*pi/3)]
Or if you really nee a loop:
syms theta;
beta = 0:2*pi/3:4*pi/3;
for x = 1:3
if x==1
L(1, :) = cos(theta+beta);
elseif x==2
L(2, :) = cos(theta+beta-2*pi/3);
else
L(3, :) = cos(theta+beta+2*pi/3);
end
end
If your array is larger, care for a pre-allocation. A cheap method would be:
for x = 3:-1:1 % Backwards for implicit pre-allocation

More Answers (0)

Categories

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

Asked:

on 4 Jun 2018

Commented:

on 5 Jun 2018

Community Treasure Hunt

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

Start Hunting!