필터 지우기
필터 지우기

How to express double arrays?

조회 수: 1 (최근 30일)
Meowooo
Meowooo 2017년 10월 21일
답변: Walter Roberson 2017년 10월 21일
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.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 10월 21일
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.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by