Indexing a variable using the slice of another matrix
이전 댓글 표시
Hello, I'm struggling to wrap my head around what I think it quite a simple problem My digi-bog model is taking an awfully long time to complete (5hours), I checked the profiler and over 3 hours of that runtime was spent on this line
%% convert Q to water stage
for i = 1:numel(river_discharge_per_channel) % 4.4e6 values (very long daily time series)
[~,index] = min(abs(Q_list-river_discharge_per_channel(i))); %Q_list changing length 2000-7000
water_depth(i) = d(index);
end
This step was being perfomed for every 20 interation of the main loop (12000 iterations, with 365 sub steps). I realised that this was not necassary and that I only needed to calculate the water stage for the next 20 years so I rewrote the above code to:
%% convert Q to water stage
for i = 1:20 % not nessary to go through the entire daily discharge list, we will only do it for 20years
river_discharge_per_channel_year = river_discharge_per_channel(lookup_year==t-1+i); %lookup_year is the year corresponding to the daily values (I made a lookup table for this) t = the main yearly loop
for j = 1:numel(river_discharge_per_channel_year)
[~,index] = min(abs(Q_list-river_discharge_per_channel_year(j)));
water_depth(river_discharge_per_channel(lookup_year==t-1+i(j))) = d(index); % this is where I'm going wrong, I want to reference the day within the yearly slice I made earlier.
end
end
It's when I try to assign the water_depth values (d) back into the daily list of water_depths, I'm not managing the index it correctly. I attempt this with this line:
water_depth(river_discharge_per_channel(lookup_year==t-1+i(j))) = d(index);
I'm not sure this is possible the way I'm trying to accomplish it, suggestions most welcome. I'm not sure the issue will be understood easily by readers, not least of becuase of my dyslexic brain, so questions most welcome.
Thanks in advance
Alex
댓글 수: 5
Jan
2021년 5월 29일
Is water_depth pre-allocated.
Alexander James
2021년 5월 29일
편집: Alexander James
2021년 5월 29일
Matt J
2021년 5월 29일
I don't see how the expression i(j) in this line
water_depth(river_discharge_per_channel(lookup_year==t-1+i(j))) = d(index);
is not producing an error. i is a scalar and cannot be indexed by j>1.
Alexander James
2021년 5월 29일
편집: Alexander James
2021년 5월 29일
Well, I think I've already given it to you in my Answer below. Once you know how to rewrite the first version of your loop, it is straightforward to rewrite your double for-loop in a similar way (however, I don't see any benefit to this):
%% convert Q to water stage
[Q_list, isort]=sort(Q_list);
d=d(isort);
for i = 1:20 % not nessary to go through the entire daily discharge list, we will only do it for 20years
index=(lookup_year==t-1+i);
water_depth(index) = interp1(Q_list,d, river_discharge_per_channel(index), 'nearest');
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!