Azzera filtri
Azzera filtri

Can you create a matrix using a for loop

3 visualizzazioni (ultimi 30 giorni)
Maryal
Maryal il 9 Dic 2017
Commentato: Lam Nguyen Van il 11 Mar 2020
Is possible to create a matrix by just using for loops Thanks
  2 Commenti
chandrapal Singh
chandrapal Singh il 9 Dic 2017
what type of matrix? can you please provide more details regarding this query?
Stephen23
Stephen23 il 9 Dic 2017
"Is possible to create a matrix by just using for loops"
Yes.

Accedi per commentare.

Risposte (2)

Amy
Amy il 11 Dic 2017
Look at the first example in the documentation for 'for': https://www.mathworks.com/help/matlab/ref/for.html

Lam Nguyen Van
Lam Nguyen Van il 10 Mar 2020
Hi,
I want to creat matrices automatically with for loop in Matlab?
phi1=30; phi2=45; phi3=90;
After running the code I want to have a matric A with the following elements:
A=[sin(phi1) cos(phi1) sin(phi1)*cos(phi1);
sin(phi2) cos(phi2) sin(phi2)*cos(phi2);
sin(phi3) cos(phi3) sin(phi3)*cos(phi3);]
  3 Commenti
Lam Nguyen Van
Lam Nguyen Van il 10 Mar 2020
Thanks!
I want to use
for i=1:3
for j=1:3
A(i,j)=...;
end
end
I want shorter code.
Lam Nguyen Van
Lam Nguyen Van il 11 Mar 2020
I did it. I used for loop and function.
phi=[30, 45, 90];
n=length(phi);
[e]=xuat_vecto(phi1);
A=zeros(n,3);
for i=1:n
[d]= xuat_vecto (phi(i));
A(i,:)=[d];
end
A
function [e]=xuat_vecto(alpha)
[a, b, c]=ham_luong_giac (alpha);
e=[a, b, c];
end
function [a1, a2, a3] = ham_luong_giac (phi)
a1=sin(phi);
a2=cos(phi);
a3=sin(phi)*cos(phi);
xuat= [a1, a2, a3];
end

Accedi per commentare.

Categorie

Scopri di più su Sparse Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by