Borrar filtros
Borrar filtros

How can I fix this matrix size error?

22 visualizaciones (últimos 30 días)
Gavin Thompson
Gavin Thompson el 13 de Sept. de 2021
Comentada: Jan el 13 de Sept. de 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 comentario
Jan
Jan el 13 de Sept. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 13 de Sept. de 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))

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by