Effacer les filtres
Effacer les filtres

How can I fix this matrix size error?

22 vues (au cours des 30 derniers jours)
Gavin Thompson
Gavin Thompson le 13 Sep 2021
Commenté : Jan le 13 Sep 2021
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 commentaire
Jan
Jan le 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.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 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))

Plus de réponses (0)

Catégories

En savoir plus sur Shifting and Sorting Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by