flow redirection in an array

조회 수: 1 (최근 30일)
Ano
Ano 2016년 5월 12일
답변: Ano 2016년 5월 12일
Dear all, I would like to perform a flow redirection for a dam of water once a condition is verified, here I am given an example to clarify the idea and the result that I would like to obtain, so you can help me figure out what is the proper way to do it. such each column is a 'door' where the flow can pass, and the rows are the time, once the flow is =10, the flow must be redirected to a new door (create a new column) and the flow at the old door must be reduced to 0, the new door is closed if the flow at the old door is below 10, otherwise it must continue within the new door, such the newData matrix must be as follow:
data= 2 1 3 4
2 1 10 4
2 1 10 4
1 3 10 10
1 2 10 10
1 2 10 4
newData= 2 1 3 4 0 0
2 1 0 4 3 0
2 1 0 4 3 0
1 3 0 0 3 4
1 2 0 0 3 4
1 2 0 4 3 0
any suggestions?! thank you!
  댓글 수: 2
Guillaume
Guillaume 2016년 5월 12일
Why do the new columns receive 3 and 4 respectively?
Ano
Ano 2016년 5월 12일
they receive the previous value that comes directly before the flow reach its limit in this case 10.

댓글을 달려면 로그인하십시오.

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2016년 5월 12일
data= [2 1 3 4
2 1 10 4
2 1 10 4
1 3 10 10
1 2 10 10
1 2 10 4]
t = data >= 10;
ii = any(t);
t0 = t(:,ii);
[m,n] = size(t0);
d = data(:,ii);
k = cumsum(t0).*t0;
v = d([k(2:end,:);zeros(1,n)] == 1);
newData = [data(:,~ii),reshape(bsxfun(@times,v(:)',cat(3,~t0,t0)),m,[])]

Ano
Ano 2016년 5월 12일
Dear Andrei Bobrov, the code that you provided works perfectly, but I would like to know how you have model it (the idea not the code because it is clear). Also I would like to mention that my biggest obstacle here is how to know which door was opened/ closed (reduced to zero) knowing that I am dealing with a quite large matrices and the matrices are evolving in function of the time. Many thanks and kind regards!

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by