how to shift trapped zeros to the bottom keeping leading zeros in given matrix fixed?

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.

 Respuesta aceptada

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)
Anew = 5×13
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

3 comentarios

Hi Matt J,
This is exactly what I need for my work. Thank you so much for your help.
But I have two questions eventhough I have already tested this code.
1.) if the column has more than one leading zeros this will work right?
2.) this will be applicable for any 2D Matrix right whatever be the size of the matrix?
Matt J
Matt J el 21 de Mzo. de 2022
Editada: Matt J el 21 de Mzo. de 2022
It should work for any matrix and for any number of leading zeros, but you should definitely test these use cases if you have any doubts.
Dear Matt,
Thank you so much for your help.
It is really working well. I really appreciate it.

Iniciar sesión para comentar.

Más respuestas (1)

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)]
output = 5×13
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 1 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 0 0 0 1 1 1 1 1 2 2 2 1 1

4 comentarios

Hi Arif, thank you so much for your immediate response, I really appreciate your help. But I have a huge matrix to deal with, it's size is ( 2160000 x 72 ). I have seen some trapped zeros but I don't even know where the zeros are in many cases. Is there any way we can run the loop in this matrix? So that it automatically shift trapped zeros in each columns to the bottom.
i think you want to move the trapped zeros only in the first column, obviously at the bottom. you can use sort function for this
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]);
C1=sort(C,'descend');
D=[B(1);C1];
output=[D A(:,2:end)]
output = 5×13
0 0 0 1 0 2 3 1 2 3 0 0 0 2 2 2 1 1 2 3 1 2 3 1 3 4 1 1 0 1 0 0 0 1 2 1 1 2 2 1 1 2 1 0 0 0 0 1 1 1 1 1 0 0 0 1 1 1 1 1 2 2 2 1 1
Actually, I want to move the trapped zeros in every column keeping all other elements fixed. 'descend' will shift '2' at the top. Also, this is the example of the matrix where we know the position of the zeros. But what is we don't know the position of zero. So I think if we can write a code in such a way that when ever it finds a zero it will shift that zeros to the bottom. I think previous one will work only when we know about the position of zeros. 'descend' will alter the position of other states like 2 3 4.
Dear Arif Hoq,
Thank you so much for your time. I really appreciate your help and for your immediate response.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de Mzo. de 2022

Comentada:

el 22 de Mzo. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by