Finding the previous consecutive entries in a matrix column
Show older comments
I have a matrix A, where upon each iteration, a new row of data is added
A = [1 0 0 1 0; 1 1 0 1 1; 0 1 0 0 1; 0 1 0 1 0; 0 1 0 1 0]; % just a matrix for example
Each 1 in each row A(i) represents a species pairing to another species, 1 in A(i,j), where j represents a nonspecific column, and pairing happens because a certain condition is met. I have this:
%Will not produce A above, just an example of my current code
iterations = 5; %random numbers for example sake
number_of_species = 10;
original_conditions = [1 0 0 1 0];
A=[original_conditions;zeros(iterations,number_of_species)]];
for j=1:number_of_species
for k = j+1:number_of_species
if condition %placeholder, my condition would take several lines of code
A(i,j) = 1;
A(i,k) = 1;
end
I need these 1's to stay paired for a certain amount of time, time_paired. Each iteration represents the change in time, dt.
So, for example, species_p = A(2,2) is paired on the second iteration. I need to keep it paired until it has been paired long enough to satisfy time_paired. After some research, I think the continue command is best for not affecting species_p until it has satisfied time_paired. But I have not been able to identify how to get a count for identifying the immediate consecutive entries in the rows, within the same column, above my current species. I do not need a count of all of the consecutive entries above my species, just the ones immediately above it. This is my main issue, and I think i could figure out the following question if I were able to figure this out.
For simplicity, let dt = 1, time paired = 2, iterations = 2: dt*iterations=time_paired.
Upon the 3rd iteration, species_p = A(3,2) has satisfied time_paired. This is not a problem because species_p would be able to be processed through the rest of my program. However, on the 4th iteration, species_p has been paired with a different species than it was in the 3rd iteration, but as my logic stands, it would be released even though it should be held for another iteration. I figured when the number of previous consecutive entries in the column (dt*iterations) exceeds time_paired, i could subtract iterations=time_paired/dt from this.
I have tried several methods proposed in answers to questions like this, but none of them fit.
4 Comments
Jan
on 21 Mar 2022
The formulation of the question is complicated. You have this input:
A = [1 0 0 1 0; ...
1 1 0 1 1; ...
0 1 0 0 1; ...
0 1 0 1 0; ...
0 1 0 1 0];
What is the wanted output? Should every 1 be continures by further 1s on the right side?
Stephen23
on 21 Mar 2022
apple3's incorrectly posted "Answer" moved here:
A is produced after 4 iterations, but I basically need a function that will tell me, at A(4,2), there are two consecutive ones directly above it. I can trouble shoot the rest.
A more general form of telling me how many 1’s are consecutively above A(i,j) would be awesome.
Sorry if I phrased this bad. I think I was trying to ask too many questions at once.
Jan
on 21 Mar 2022
Programming means to get a simple abstract representation of the problem. So for the given example input:
A = [1 0 0 1 0; ...
1 1 0 1 1; ...
0 1 0 0 1; ...
0 1 0 1 0; ...
0 1 0 1 0];
the wanted output is:
B = [0 0 0 0 0; ...
1 0 0 1 0; ...
0 1 0 0 1; ...
0 2 0 0 0; ...
0 3 0 1 0];
Then:
B = max(cumsum(A, 1) - 1, 0);
Or should B(4,3) be 2 and B(5,3) be 3? This would mean, that the cummulative sum is reset, when the value is 0.
Do you see, that the wanted procedure can be explained much more compact?
apple3
on 21 Mar 2022
Answers (1)
Jan
on 21 Mar 2022
AS far as I understand the discussion, this solves your needs:
B = max(cumsum(A, 1) - 1, 0);
Categories
Find more on Linear Algebra 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!