MATLAB Answers

Use Position Values to Keep Values in Matrix

19 views (last 30 days)
Derrick Vaughn
Derrick Vaughn on 20 Apr 2021 at 15:11
Commented: David Hill on 20 Apr 2021 at 16:32
Hi all, this is a little confusing to explain so I'll write down what I'm trying to do and then give a shorter example that may be helpful for generating the code.
Long version:
I have a matrix called Filling of dimensions 10834 x 500, which indicates how much sediment is deposited within a river channel. A separate matrix, Volume, has the same dimensions as Filling (10834 x 500) but indicate when sediment load is below or exceeds the volume of the channel. When the load is below the channel volume, the value of the cell within the matrix Volume is positive but when the load is higher than the channel volume, the value within the cell of matrix Volume is negative. Based on the positive and negative values within the matrix Volume, I then created an array called Position that indicates which column has the switch from positive to negative values for every row in the matrix Volume. This gives the array Position dimensions of 10834 x 1.
I now want to take the values with the array Position (10834 x 1) and apply it to the matrix Filling (10834 x 500) so that the value within Position results in keeping the value of the corresponding column while making the other values within the row 0. I would like to call the resulting matrix Filled and have it be of dimensions 10834 x 500. Each row within the matrix Filled should thus only have one value with all other columns of that row being 0. I've tried doing this through indexing but each possible combination I've tried doesn't result in what I'm wanting.
Short version:
To make this slightly easier, let's say the first matrix Filling is of size 5 x 3:
Filling=[1 2 3; 4 5 6; 7 8 9; 1 2 3; 4 5 6]
The other matrix, Volume, is also of size 5 x 3, but contains positive and negative values:
Volume=[1 -1 -2; 1 -1 -2; 2 1 -1; 3 2 -1; 1 -1 -2]
The array, Position, is 5 x 1 and indicates which column within Volume has the switch from positive to negative values:
Position=[2;2;3;3;2]
I now want to apply the array Position to the matrix Filling so that the values within the column that corresponds to the value within Position is kept while all other values become 0. I want to call this new matrix Filled and the resulting dimensions should be 5 x 3. The end result of Filled should look something like this:
Filled=[0 2 0; 0 5 0; 0 0 9; 0 0 3; 0 5 0]
To repeat, how can I end up with the new matrix called Filled by using the existing matrix called Filling and the existing array called Position? Thanks in advance!

Accepted Answer

David Hill
David Hill on 20 Apr 2021 at 15:31
Filled=Filling.*(cumsum(Volume<0,2)==1);
  2 Comments
David Hill
David Hill on 20 Apr 2021 at 16:32
Just set the last column of Volume to -1. Then run the code above.
Volume(:,end)=-1;

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by