How can I fix this matrix size error?

89 visualizzazioni (ultimi 30 giorni)
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 Commento
Jan
Jan il 13 Set 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.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 13 Set 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))

Più risposte (0)

Categorie

Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by