Writing a function that merges two sorted vectors

7 ビュー (過去 30 日間)
Lee Cohen
Lee Cohen 2018 年 4 月 21 日
コメント済み: Ameer Hamza 2018 年 4 月 22 日
Hello, I need to write a function that merge two sorted vectors to one sorted vector that containes all the values.
My code is:
vec1= input('enter vec1\n');
vec2= input('enter vec2\n');
vec1=sort(vec1)
vec2=sort(vec2)
function [res]=merge(vec1, vec2)
n1=length(vec1);
n2=length(vec2);
n=n1+n2;
res=zeros(n, 1);
if n1==0
res=vec2;
elseif n2==0
res=vec1; else
i1=1;
i2=1;
end
for j=1:n
if i1>n1
res(j)=vec2(i2);
i2=i2+1;
elseif i2>n2 res(j)=vec(i1); i1=i1+1;
elseif vec(i1)<vec(i2)
res(j)=vec(i1);
i1=i1+1;
else
res(j)=vec2(i2);
i2=i2+1;
end
end
end
My questions are: Is my code right and how can I call the new function that I created? (I saved the file named 'merge' in a function file).
  1 件のコメント
Ameer Hamza
Ameer Hamza 2018 年 4 月 21 日

First of all, you should format your code properly: https://www.mathworks.com/matlabcentral/answers/7885-tutorial-how-to-format-your-question so that others can easily read it.

There are some mistakes in your merge function. You are using vec on some lines instead of vec1 or vec2, so it is difficult for me to understand what do mean by merging two sorted vectors. Still, I have tried to answer this question according to my understanding of this code. You can see it in answer section below.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 4 月 21 日

If you simply want to join two sorted vectors you can use

new_vec = [vec1 vec2];

but this seems improbable to me since joining two sorted vectors like this may not be very useful. The second possibility is, you also want to sort the result, you can do that as

new_vec = sort([vec1 vec2]);

In this second case, you don't even need to run

vec1=sort(vec1)
vec2=sort(vec2)

because the command I mentioned will sort both of them together.

In either case, no need to write your own merge function. You can use MATLAB builtin function to reach same results.

  2 件のコメント
Lee Cohen
Lee Cohen 2018 年 4 月 21 日
編集済み: Lee Cohen 2018 年 4 月 21 日

Thank you for the answer, I know this Matlab function, but my task is to write a new function that merges those vectors. Here is my fixed code:

vec1= input('enter vec1\n');

vec2= input('enter vec2\n');

vec1=sort(vec1)

vec2=sort(vec2)

function [res]=merge(vec1, vec2)

n1=length(vec1);

n2=length(vec2);

n=n1+n2;

res=zeros(n, 1);

if n1==0

res=vec2;

elseif n2==0

res=vec1; else

 i1=1;
 i2=1;

end

for j=1:n

if i1>n1

res(j)=vec2(i2);

i2=i2+1;

elseif i2>n2

res(j)=vec1(i1);

i1=i1+1;

 elseif  vec1(i1)<vec2(i2)
        res(j)=vec1(i1);
        i1=i1+1;
    else
        res(j)=vec2(i2);
        i2=i2+1;
    end
end
end

I would like to know if there is somthing wrong with it.

Ameer Hamza
Ameer Hamza 2018 年 4 月 22 日

Yes, Your function is giving correct output. You can just try by calling the function like this

merged_vector = merge(vec1, vec2);

You can add this line before the definition of the function merge.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by