How to use output numbers as a vector.

조회 수: 5 (최근 30일)
Brendan Clark
Brendan Clark 2021년 4월 6일
댓글: Brendan Clark 2021년 4월 6일
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
Brendan Clark
Brendan Clark 2021년 4월 6일
I'm new to this, and in all honesty I'm really not that great with it. I essentially am required to sort the vector without using sort. Your probably right that I can't use unique as well. My teacher just has me use google to learn syntax and I end up with weird ways of getting things done because I'm just trying to adapt what I can find thats similar. The assignment doesn't require that I change to characters.
Brendan Clark
Brendan Clark 2021년 4월 6일
I'm just trying to start over from the beginning for the time being.

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2021년 4월 6일
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.

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 4월 6일
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
Rik
Rik 2021년 4월 6일
This code only works as intended because d is a column vector, all elements of a are integers, and there are no duplicate values.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by