Saving only the last loop
Mostra commenti meno recenti
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.
Risposte (1)
KALYAN ACHARJYA
il 18 Dic 2019
Modificato: KALYAN ACHARJYA
il 18 Dic 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 Commento
Neill Harris
il 18 Dic 2019
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!