Saving only the last loop
Afficher commentaires plus anciens
Hi, I'm very new to MATLAB (introduced to me this first term at uni), and I have been struggling with trying to save each loop in a column that is preallocated as 135x1 using zeros()
This is my code for the section I am stuck on:
%% Preallocating memory
dischargemax=zeros((winterdischarge(end,1)-winterdischarge(1,1)),1);
yearcolumn=winterdischarge(:,1);
%% Looping to find max of each year (Full year of 1883 not given, only months Nov and Dec.)
%% 1883
dischargemax(1,1)=max(winterdischarge(yearcolumn==1883,4));
%% other years
for i=2:length(leapyear)
for ii = 1884:2018
extract=winterdischarge(yearcolumn == ii,4);
end
dischargemax(i,1)=max(extract);
end
In ans I have the first max of the first year in (1,1) and then every other row is the max extract of the last year. How would I fix this? Also how would I set the output to dischargemax instead of ans?
Thank you.
Réponses (1)
KALYAN ACHARJYA
le 18 Déc 2019
Modifié(e) : KALYAN ACHARJYA
le 18 Déc 2019
Do the proper indexing
May be
for i=2:length(leapyear)
for ii = 1884:2018
extract(ii)=winterdischarge(yearcolumn==ii,4);
end
dischargemax(i)=max(extract);
end
See the following example-
for k=1:n
result=...??
end
This code reflects the the last iteration results only (Result having last data), otherwise
for k=1:n
result(k)=...??
end
It holds the all output in result 1 D array, do as per your requiremnets.
Any issue let me know.
1 commentaire
Neill Harris
le 18 Déc 2019
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!