Sort Matrix Array and skip zeros.

조회 수: 9 (최근 30일)
Kalle
Kalle 2014년 9월 1일
댓글: Kalle 2014년 9월 2일
I have an array as this:
Array1 = [1 2 3 4 0 0;
1 2 3 0 0 0;
1 0 0 0 0 0;
1 2 0 0 0 0];
Array1 is sorted and is fine as it is. But lets say I type in a mistake like this:
Array2 = [2 1 3 4 0 0;
1 2 3 0 0 0;
1 0 0 0 0 0;
1 2 0 0 0 0];
Now coulomb 1 in Array2 is not sorted and I would like to sort it. But when I try to sort it with the function sort(Array2(1,:)) the zeros (0) will be listed first. Its easy to understand why it does this because 0 is smaller then 1,2,3,4, etc. It will then look like:
Array2 = [0 0 1 2 3 4;
1 2 3 0 0 0;
1 0 0 0 0 0;
1 2 0 0 0 0];
But I would like the array to look like Array1:
Array2 = [1 2 3 4 0 0;
1 2 3 0 0 0;
1 0 0 0 0 0;
1 2 0 0 0 0]
How is this possible? Can I somehow sort an array and skip the zeros?
Your Sincerely.
  댓글 수: 4
Guillaume
Guillaume 2014년 9월 2일
Adam's or the second option in my answer will give you that.
Kalle
Kalle 2014년 9월 2일
Ye I noticed. Thanks. :)

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

채택된 답변

Adam
Adam 2014년 9월 1일
Array1( Array1 == 0 ) = NaN;
Array2 = sort( Array1, 2 );
Array2( isnan( Array2 ) ) = 0;
works if you want 0s shuffled to the end, though not if you want them to remain exactly where they are.
  댓글 수: 2
Kalle
Kalle 2014년 9월 2일
This is exactly what I was looking for. Thanks. :)
Kalle
Kalle 2014년 9월 2일
Right now I use the zeros for filling the Matrix and in the end I need to implement it in a c function and test it in a lab. Nice and clean way to sort the Array. Thanks.

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

추가 답변 (3개)

Guillaume
Guillaume 2014년 9월 1일
One possible way:
for row = 1:size(Array2, 1)
Array2(row, 1:nnz(Array2(row, :))) = nonzeros(sort(Array2(row, :)));
end
Or if zeros are not always at the end in the original matrix:
ncol = size(Array2, 2);
for row = 1:size(Array2, 1)
Array2(row, :) = [nonzeros(sort(Array2(row, :))); zeros(ncol - nnz(Array2(row, :)), 1)];
end
  댓글 수: 1
Kalle
Kalle 2014년 9월 2일
Thanks for your time, but Adam answered my question. :)

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


Ilham Hardy
Ilham Hardy 2014년 9월 1일
If you know the starting value (say sort start from 1, instead of 0):
tblA = [2 1 3 4 0 0];
vStartSort = 1
tblA = sort(tblA);
tblA = circshift(tblA, [1, -find(tblA == vStartSort, 1) + 1]);
If you just want to skip zero (you don't know the smallest value after zero in array)
tblA = [2 1 3 4 0 0];
tblA = sort(tblA);
minGreaterThanZero = tblA(tblA>0);
vStartSort = minGreaterThanZero(1);
tblA = circshift(tblA, [1, -find(tblA == vStartSort, 1) + 1]);
HTH, IH
  댓글 수: 1
Kalle
Kalle 2014년 9월 2일
Thanks for your time Ilham Hardy. Adam gave me exactly what I was looking for. Maybe that wasnt so clear after all. But thanks anyways. :) Always nice to learn other methods.

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


Image Analyst
Image Analyst 2014년 9월 1일
This will give you exactly what you want, with zeros remaining in the original locations.
Array2 = [2 1 3 4 0 0;
1 2 3 0 0 0;
1 0 0 0 0 0;
1 2 0 0 0 0]
% Find locations of non-zeros.
% Transpose array first so we can sort column-major fashion.
nonZeroIndexes = Array2(:).'~=0
% Extract the values from those locations.
nonZeroValues = Array2(nonZeroIndexes)
% Sort them.
sortedValues = sort(nonZeroValues)
% Get an array with the zeros in their original locations.
out = Array2(:); % Initialize
% Assign only those elements in non-zero locations.
% This will still be a column vector.
out(nonZeroIndexes) = sortedValues(:)
% Reshape to original 2D size of Array2.
out = reshape(out, size(Array2))
In command window.
out =
1 2 3 4 0 0
1 2 3 0 0 0
1 0 0 0 0 0
1 2 0 0 0 0
  댓글 수: 1
Kalle
Kalle 2014년 9월 2일
This is a great solution if the zeros should be in the original locations. Right now I need the zeros to be placed in the end of the Matrix, since the zeros is only for filling the matrix. Adam gave me a simple answer to this. Thanks for your time though.

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

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by