Finding sorted rows in a matrix
Mostrar comentarios más antiguos
Hi,
I need to know which rows of a matrix are sorted in ascending without using the "for" cycle. For instance, if i have:
outsx = [
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3]
i want a vector containing indexes of which rows are sorted in ascending order. In this case.
ii= [1 4 5 7 8 9]
Thank you in advance
Respuestas (3)
the cyclist
el 20 de Mzo. de 2021
For my current understanding of what you want, I believe this works:
ii = find(diff(outsx,[],2)>=0)
2 comentarios
Jan
el 20 de Mzo. de 2021
I assume an all() is wanted:
ii = find(all(diff(outsx,[],2) >= 0, 2))
the cyclist
el 21 de Mzo. de 2021
Yes, thanks for catching that
the cyclist
el 20 de Mzo. de 2021
Editada: the cyclist
el 20 de Mzo. de 2021
outsx = [
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3];
ii = find(outsx(:,2)>=outsx(:,1))
1 comentario
Marco Gaetani d'Aragona
el 20 de Mzo. de 2021
Star Strider
el 20 de Mzo. de 2021
Two approaches:
for k = 1:size(outsx,1)
TF(k,:) = issorted(outsx(k,:),2,'ascend')
end
Result = find(TF)
TF = outsx(:,2) >= outsx(:,1);
Result = find(TF)
both producing:
Result =
1
4
5
7
8
9
.
2 comentarios
Marco Gaetani d'Aragona
el 20 de Mzo. de 2021
the cyclist
el 20 de Mzo. de 2021
FYI, just because code has a for loop does not necessarily mean it is slower than other solutions.
Finding a solution that works is the first step (partly to verify that the solution does what you want), and then thinking about readability and optimization follows.
Categorías
Más información sobre Shifting and Sorting Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!