Filter löschen
Filter löschen

How can I fix this matrix size error?

22 Ansichten (letzte 30 Tage)
Gavin Thompson
Gavin Thompson am 13 Sep. 2021
Kommentiert: Jan am 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 Kommentar
Jan
Jan am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by