How to write array results of variable length to zeros matrix matrix?

6 views (last 30 days)
Hi!
I'm trying to find the most efficient way to write arrays to a zeros matrix. I have another function that creates the array, now I'm just trying to figure out the most efficient way to store it in a matrix. I also need to lag the arrays by 1 column and 1 row for subsequent arrays as well. For those of you who are familiar with Unit Hydrograph convolution, that's what I'm doing. The reason I need this to be as fast as possible is because I'm going to be running this code for like 20,000 time steps. Also, I should mention I'm a Matlab Noob.
Here's the array I've already calculated:
I want to put it in this zeros matrix:
What I want to output is this:
The reason I need to put the arrays in a matrix is because I need to sum all of the rows across the columns and place into a new array. I should also mention that the arrays might be variable lengths and not all will be the same magnitude, as shown here, during actual implementation of the code depending on the storm event.
Thanks for any help you can provide!
  4 Comments
tyuky
tyuky on 23 Apr 2020
Also it looks like the picture of the output is slightly incorrect as you pointed out. The first value should be zero.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 23 Apr 2020
Edited: Adam Danz on 23 Apr 2020
From the example inputs/outputs given in the quesiton, I don't know what's happening to the first 0 at index 1 which doesn't appear in the outputs. I also don't know where the last non-zero value in the output comes from since it differs from the last non-zero value in the input.
Assuming this is an error, here's how to produce that matrix using circshift.
data = [rand(25,1);0;0;0]; % input vector
M = cell2mat(arrayfun(@(i){circshift(data(:),i)},0:3));% output matrix
To get the sum of each row,
sum(M,2)
It also may be useful for you to explore the movsum() function.
  10 Comments
tyuky
tyuky on 23 Apr 2020
Hi Adam,
I ended up changing the code, but it worked out. Here's how we were able to get this done. The end result is just an array of the summed matrix.
Thanks again for your help!
% read in peak runoff
n = length(Hbd);
Interflow = zeros(n,1);
% Read in Hbd
for j = 1:n
HbdIn=Hbd(j);
% Run the baseflow code
Base = BaseflowInterflowUH(HbdIn,5,10000,0.7);
% Add together interflow into one time series
Interflow([j:lastReplace],1)=Interflow([j:lastReplace],1)+Base([1:lastBase],1);
end

Sign in to comment.

More Answers (0)

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!