How to use output numbers as a vector.
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Brendan Clark
el 6 de Abr. de 2021
Comentada: Brendan Clark
el 6 de Abr. de 2021
The code I'm working on does what it's supposed to, however, I technically want it to output the values as a vector b instead of a message with the numbers.
The goal of this function is to input a vector and have the program sort them from smallest to largest, but without using the sort function.
The code I'm using is
clear variable
clc
a=input('Please input a vector of integers');
c=length(a);
for d = unique(a(:))
b=(sprintf('%d',d));
end
clc
sprintf('b= ')
disp(b)
right now the answer I'm getting is:
ans =
'b= '
2469
this is based upon the vector [9 4 6 2]
What I want to see output is:
b =
2 4 6 9
this output was copied from my teachers p code and is technically what my output should look like.
Yes this is homework.
3 comentarios
Respuesta aceptada
Rik
el 6 de Abr. de 2021
There are may sorting algorithms you could implement. One of the easiest to implement (and one of the slowest) is called bubble sort. If you Google 'bubble sort Matlab' you will find several implementations. Otherwise I would suggest reading some Wikipedia pages and trying to implement it yourself.
Más respuestas (1)
Mathieu NOE
el 6 de Abr. de 2021
hello
see bleow :
clear variable
clc
a=input('Please input a vector of integers : ');
c=length(a);
for d = unique(a(:))
b=(sprintf('%d ',d)); % add a blank after "%d"
end
clc
sprintf('b = %s', b)
1 comentario
Rik
el 6 de Abr. de 2021
This code only works as intended because d is a column vector, all elements of a are integers, and there are no duplicate values.
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!