Azzera filtri
Azzera filtri

Stor data in a vector

3 visualizzazioni (ultimi 30 giorni)
Lukas
Lukas il 5 Giu 2013
For every loop z i want to store my data so that JJ doesnt just become the reslut of z=40, i want JJ to be a vector(1,1000), now i only get JJ to be a vector of (1,25) from the last z loop.
p_t2=1:1000 pt2=1:40
for z=1:40 for v=1:25 XX=p_t2(v)./pt2; JJ(v)=sum(XX); end p_t2(1:25)=[]; pt2(:,1)=[]; end
thank you in advance

Risposta accettata

Iman Ansari
Iman Ansari il 5 Giu 2013
p_t2=1:1000
pt2=1:40
JJ=[];
for z=1:40
for v=1:25
XX=p_t2(v)./pt2;
JJ=[JJ sum(XX)];
end
p_t2(1:25)=[];
pt2(:,1)=[];
end
  1 Commento
Lukas
Lukas il 5 Giu 2013
thank you for a very quick answer.

Accedi per commentare.

Più risposte (3)

Lukas
Lukas il 5 Giu 2013
The code didnt look as i wanted it, sorry...
p_t2=1:1000
pt2=1:40
p_t2=1:1000
pt2=1:40
for z=1:40
for v=1:25
XX=p_t2(v)./pt2;
JJ(v)=sum(XX);
end
p_t2(1:25)=[];
pt2(:,1)=[];
end

Mark
Mark il 5 Giu 2013
Modificato: Mark il 5 Giu 2013
If you want preallocation:
p_t2=1:1000;
pt2=1:40;
JJ=zeros(1,1000);
for z=1:40
for v=1:25
XX=p_t2(v)./pt2;
JJ((z-1)*25+v)=sum(XX);
end
p_t2(1:25)=[];
pt2(:,1)=[];
end

Lukas
Lukas il 5 Giu 2013
Thank you for the quick answers, however now I have an other problem..
How do I in a spimle way adjust it to be able to hadel a matrix as input, for example if:
p_t2=rand(2,1000)
pt2=rand(2,40)
JJ=[];
for z=1:40
for v=1:25
XX=p_t2(v)./pt2;
JJ=[JJ sum(XX)];
end
p_t2(1:25)=[];
pt2(:,1)=[];
end
I want the output JJ be a matrix with dimension (2,1000)
  1 Commento
Iman Ansari
Iman Ansari il 5 Giu 2013
clear;
p_t2=rand(2,1000);
pt2=rand(2,40);
JJ=[];
for z=1:40
for v=1:25
XX=bsxfun(@rdivide,p_t2(:,v),pt2);
JJ=[JJ sum(XX,2)];
end
p_t2(:,1:25)=[];
pt2(:,1)=[];
end

Accedi per commentare.

Categorie

Scopri di più su Programming 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