Borrar filtros
Borrar filtros

How can I store the values in a matrix while using for loop?

2 visualizaciones (últimos 30 días)
Tayyab Mehmood
Tayyab Mehmood el 12 de Jul. de 2017
Editada: Andrei Bobrov el 12 de Jul. de 2017
Hi, I am a bit confused, how to store the values in a matrix while using for loop. Here is the my code
k=1.5:0.1:3;
ws=zeros(1,length(k));
dof=zeros(1,length(k));
for i=1.5:0.1:3
ws=22+4+7+i;
dof(i)=2+ ws;
end
I want to store the values pf 'ws' and 'dof' in matrix. Thanks

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 12 de Jul. de 2017
Editada: Andrei Bobrov el 12 de Jul. de 2017
k=1.5:0.1:3;
n = numel(k);
ws=zeros(1,n);
dof=zeros(1,n);
for ii=1:n
ws(ii)=22+4+7+k(ii);
dof(ii)=2+ ws(ii);
end
or just
ws = 33 + k;
dof = ws + 2;

alice
alice el 12 de Jul. de 2017
You have to give the position where you want to write, like this:
k = 1.5:0.1:3;
ws = zeros(1,length(k));
dof = zeros(1,length(k));
for cpt = 1:length(k)
ws(cpt) = 22+4+7+k(cpt);
dof(cpt)= 2+ws(cpt);
end
But you don't need a loop to do this and it would be better to do simply:
k = 1.5:0.1:3;
ws = 22+4+7+k;
dof = 2+ws;

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by