Clear Filters
Clear Filters

How can I fix this matrix size error?

22 views (last 30 days)
function [outMat] = CustomSort( inMat )
% Takes a 2D array inMat with random integers between (inclusively) -100 and 100,
% and produces a n by m array outMat that consists of sorts values
% in inMat such that the lowest value in inMat is the (1,1) position
% in outMat, and order the remaining elements left to right on
% the first row till last column, starting with the next row and so on.
% Your code goes here %
outMat = reshape(sort(inMat,2),size(inMat))
end
How can I fix the sizing of this(see error below)? I tried transposing it but it still hasn't worked. Any help is appreciated :)
  1 Comment
Jan
Jan on 13 Sep 2021
The text of the question isn strange. What does "consists of sorts values" mean? "till last column, starting with the next row and so on" is puzzling also. The variables m and n are not explained.
A clear question is much easier to solve. In your code, the reshape has no effect.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 13 Sep 2021
That test is broken. According to the comments you only need to work with integers, but that test is passing in floating point numbers in the range -1/2 to +1/2
Also, your solution is not correct. The smallest value out of the complete input array should go in the first output position, the second should go into the next column, and so on. You, on the other hand, are sorting row by row.
It is a bit difficult to tell, but it looks to me as if the number of rows of inputs has to become the number of columns of output. If so then size(inMat) would not be correct, and you would need size(inMat.') or fliplr(size(inMat))

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!