how to shift trapped zeros to the bottom keeping leading zeros in given matrix fixed?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sushil Pokharel
am 21 Mär. 2022
Kommentiert: Sushil Pokharel
am 22 Mär. 2022
Hi all, I have a problem while finding the probablity transition matrix. The code I have written to find the transition matrix calculate the transition probability matrices of each columns (in the given matrix below) but it excludes the transition between two states if zeros are trapped like
1
0
2 (shown below in bold) So, I wanted shift the trapped zeros to last.
I encountered trapped zeros in the matrix for instance, a matrix like
A = [0 0 0 1 0 2 3 1 2 3 0 0 0;
1 2 2 1 1 2 3 1 2 3 1 3 4;
0 1 0 1 0 0 0 1 2 1 1 2 2;
2 1 2 1 0 0 0 0 1 1 1 1 1;
1 0 0 1 1 1 1 1 2 2 2 1 1]
Now, I want to change this matrix into new matrix like
new_A =[0 0 0 1 0 2 3 1 2 3 0 0 0;
1 2 2 1 1 2 3 1 2 3 1 3 4;
2 1 2 1 1 1 1 1 2 1 1 2 2;
1 1 0 1 0 0 0 1 1 1 1 1 1;
0 0 0 1 0 0 0 0 2 2 2 1 1]
shifting all trapped zeros to the bottom of each column. Any help will be greatly appreciated.
0 Kommentare
Akzeptierte Antwort
Matt J
am 21 Mär. 2022
Bearbeitet: Matt J
am 21 Mär. 2022
A=[ 0 0 0 1 0 2 3 1 2 3 0 0 0
1 2 2 1 1 2 3 1 2 3 1 3 4
0 1 0 1 0 0 0 1 2 1 1 2 2
2 1 2 1 0 0 0 0 1 1 1 1 1
1 0 0 1 1 1 1 1 2 2 2 1 1];
B=A>0;
C=cummax(B,1);
D=double(B);
D(C&A==0)=inf;
D=sort(D,1);
D=D~=0 & ~isinf(D);
Anew=zeros(size(A));
Anew(D)=A(A~=0)
3 Kommentare
Weitere Antworten (1)
Arif Hoq
am 21 Mär. 2022
if your matrix A almost same dimension( 5 X 13) everytime, then
A = [0 0 0 1 0 2 3 1 2 3 0 0 0;
1 2 2 1 1 2 3 1 2 3 1 3 4;
0 1 0 1 0 0 0 1 2 1 1 2 2;
2 1 2 1 0 0 0 0 1 1 1 1 1;
1 0 0 1 1 1 1 1 2 2 2 1 1];
B=A(:,1);
C=B(2:end);
C([2 end])=C([end 2]);
D=[B(1);C];
output=[D A(:,2:end)]
4 Kommentare
Siehe auch
Kategorien
Mehr zu Vehicle Network Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!