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.

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 12월 18일
편집: KALYAN ACHARJYA 2019년 12월 18일

0 개 추천

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

Neill Harris
Neill Harris 2019년 12월 18일
Hi,
Thank you for the speedy reply.
After implementing your code I get the following error.
Subscripted assignment dimension mismatch.
Error in MaxDischargeWinter (line 22)
extract(ii)=winterdischarge(yearcolumn==ii,4);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 12월 18일

댓글:

2019년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by