How to express double arrays?
1 view (last 30 days)
Show older comments
My question is asking:
f u n c ti o n combinedArray = combine2Arrays( a r ray1 , a r r a y 2 )
Where: e array1 is a 1 by n sorted double array, and array2 is a 1 by m sorted double array.
How do I define these variables? Thanks a lot.
Also, how to define combine2Arrays too?
The whole question looks like this:
In this exercise, you will implement the merge sort algorithm to sort a unsorted 1-d double array.
1. Write a nested function called combine2Arrays with the following function signature:
1 f u n c ti o n combinedArray = combine2Arrays( a r ray1 , a r r a y 2 )
where array1 is a 1 by n sorted double array, and array2 is a 1 by m sorted double array, your function combine2Arrays should return combinedArray, the combined and sorted array of array1 and array2.
For example, combine2Arrays([1, 3, 5], [2, 4]) should return [1, 2, 3, 4, 5].
2. Write a function called mergeSort with the following function signature:
1 f u n c ti o n s o r t e dA r r a y = mergeSort ( doubleArray )
where doubleArray is a 1 by N unordered double array to be sorted, sortedArray is the sorted array of doubleArray. Merge sort splits the array into two sub-arrays, recursively sorts the two sub-arrays, and then combines the two sorted sub-arrays. For example, merge sort first splits the array [10, 8, 3, 8, 6, 1] into 2 sub-arrays: [10, 8, 3] and [8, 6, 1]. It then sorts the 2 sub-arrays recursively and gets [3, 8, 10] and [1, 6, 8]. Finally, it combines these two sorted sub-arrays and return [1, 3, 6, 8, 8, 10]. Use the nested function combine2Arrays you developed to combine the two sorted sub-arrays.
0 Comments
Answers (1)
Walter Roberson
on 21 Oct 2017
v1 = [1, 3, 5]; %expresses the double array containing [1, 3, 5]
v2 = [2, 4]; %expresses the double vector containing [2, 4]
combine2Arrays(v1, v2)
Inside your function, you do not do anything to tell MATLAB that they are double arrays: it already knows based upon what you passed.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!