Indexing a variable using the slice of another matrix
Show older comments
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 Comments
Jan
on 29 May 2021
Is water_depth pre-allocated.
Alexander James
on 29 May 2021
Edited: Alexander James
on 29 May 2021
Matt J
on 29 May 2021
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
on 29 May 2021
Edited: Alexander James
on 29 May 2021
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
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!