两个排序数组求中值

http://blog.youkuaiyun.com/abcjennifer/article/details/7291758
题目描述:

    Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the non-decreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
    Given two increasing sequences of integers, you are asked to find their median.

输入:

    Each input file may contain more than one test case.
    Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤1000000) is the size of that sequence. Then N integers follow, separated by a space.
    It is guaranteed that all the integers are in the range of long int.

输出:

    For each test case you should output the median of the two given sequences in a line.

样例输入:
4 11 12 13 14
5 9 10 15 16 17
样例输出:

13


解法一:

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "stdio.h"  
  3. #include "math.h"  
  4. #include "map"  
  5. #include "vector"  
  6. #include "queue"  
  7. #include "memory.h"  
  8. #include "algorithm"  
  9. #include "string"  
  10. using namespace std;  
  11. #define N 1000005  
  12. #define INF 1<<29  
  13. #define max(a,b) a>b?a:b  
  14.   
  15. int a[N],b[N];  
  16. int array3[N];  
  17.   
  18. int MergeArray(int la,int ha,int lb,int hb)  
  19. {  
  20.     int i,j,x;  
  21.     i=la,j=lb,x=0;  
  22.     while(i<=ha&&j<=hb)  
  23.     {  
  24.         if(a[i]>b[j])   array3[x]=b[j++];  
  25.         else    array3[x]=a[i++];  
  26.         x++;  
  27.     }  
  28.     while(i<=ha) array3[x++]=a[i++];  
  29.     while(j<=hb)array3[x++]=b[j++];  
  30.     x--;  
  31.     return array3[x/2];  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     int m,n,i,j;  
  37.     while(scanf("%d",&n)!=EOF)  
  38.     {  
  39.         for(i=0;i<n;i++)          
  40.             scanf("%d",&a[i]);  
  41.         cin>>m;  
  42.         for(j=0;j<m;j++)  
  43.             scanf("%d",&b[j]);  
  44.         int la,lb,ha,hb,ma,mb;  
  45.         la=lb=0;ha=n-1;hb=m-1;  
  46.         int res;  
  47.         int len=1;  
  48.         while(len>0)  
  49.         {  
  50.             ma=(la+ha)>>1;mb=(lb+hb)>>1;  
  51.             if(a[ma]==b[mb])  
  52.             {  
  53.                 res=a[ma];  
  54.                 break;  
  55.             }  
  56.             if(a[ma]>b[mb])  
  57.             {  
  58.                 len=min(ha-ma,mb-lb);  
  59.                 ha-=len;  
  60.                 lb+=len;  
  61.             }  
  62.             else if(a[ma]<b[mb])  
  63.             {  
  64.                 len=min(ma-la,hb-mb);  
  65.                 la+=len;  
  66.                 hb-=len;           
  67.             }  
  68.         }  
  69.         if(a[ma]==b[mb])  
  70.         {  
  71.             printf("%d\n",res);  
  72.             continue;  
  73.         }  
  74.         res=MergeArray(la,ha,lb,hb);  
  75.         printf("%d\n",res);  
  76.     }  
  77. }  

或者,其实这道题目不进行main中的那个优化也行的。
[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "stdio.h"  
  3. #include "math.h"  
  4. #include "map"  
  5. #include "vector"  
  6. #include "queue"  
  7. #include "memory.h"  
  8. #include "algorithm"  
  9. #include "string"  
  10. using namespace std;  
  11. #define N 1000005  
  12. #define INF 1<<29  
  13. #define max(a,b) a>b?a:b  
  14.   
  15. int a[N],b[N];  
  16. int array3[N];  
  17.   
  18. int MergeArray(int la,int ha,int lb,int hb)  
  19. {  
  20.     int i,j,x;  
  21.     i=la,j=lb,x=0;  
  22.     while(i<=ha&&j<=hb)  
  23.     {  
  24.         if(a[i]>b[j])   array3[x]=b[j++];  
  25.         else    array3[x]=a[i++];  
  26.         x++;  
  27.     }  
  28.     while(i<=ha) array3[x++]=a[i++];  
  29.     while(j<=hb)array3[x++]=b[j++];  
  30.     x--;  
  31.     return array3[x/2];  
  32. }  
  33.   
  34. int main()  
  35. {  
  36.     int m,n,i,j;  
  37.     while(scanf("%d",&n)!=EOF)  
  38.     {  
  39.         for(i=0;i<n;i++)          
  40.             scanf("%d",&a[i]);  
  41.         cin>>m;  
  42.         for(j=0;j<m;j++)  
  43.             scanf("%d",&b[j]);  
  44.         int la,lb,ha,hb,ma,mb;  
  45.         la=lb=0;ha=n-1;hb=m-1;  
  46.         int res;  
  47.           
  48.         res=MergeArray(la,ha,lb,hb);  
  49.         printf("%d\n",res);  
  50.     }  
  51. }  


解法2:(利用algorithm 中的nth_element())

[cpp]  view plain copy
  1. #include "iostream"  
  2. #include "stdio.h"  
  3. #include "math.h"  
  4. #include "map"  
  5. #include "vector"  
  6. #include "queue"  
  7. #include "memory.h"  
  8. #include "algorithm"  
  9. #include "string"  
  10. using namespace std;  
  11. #define N 1000005  
  12. #define INF 1<<29  
  13. #define max(a,b) a>b?a:b  
  14.    
  15. long a[1000000];  
  16. int main()  
  17. {  
  18.        long n1,n2,i,mid;  
  19.         while(cin>>n1)  
  20.         {  
  21.               for(i=0;i<n1;i++)  
  22.                    cin>>a[i];  
  23.               cin>>n2;  
  24.               for(;i<n1+n2;i++)  
  25.                    cin>>a[i];  
  26.               mid=(i-1)/2;  
  27.               nth_element(a,a+mid,a+i);  
  28.               cout<<a[mid]<<endl;  
  29.          }  
  30.     return 0;  
<think>嗯,用户想了解如何用MATLAB一维数组中值和IQR。首先,我得回忆一下MATLAB里相关的函数。中值的话,应该用median函数,这个我记得没错。那IQR是四分位间距,也就是上四分位数和下四分位数的差。MATLAB有没有直接计算IQR的函数呢?好像有,应该是iqr函数,但不确定是否需要统计工具箱。可能需要确认一下。 接下来,用户可能不太清楚具体步骤,所以需要分步骤说明。首先是如何计算中值,然后是如何计算IQR。如果用户没有安装统计工具箱,可能需要手动计算四分位数,用quantile函数。比如,上四分位数是75%分位数,下四分位数是25%分位数,两者相减就是IQR。 然后,得考虑用户的数据是什么样的数组。一维数组可能是一个行向量或列向量,但median和quantile函数应该都能处理。需要注意示例代码的正确性,比如数据是否正确输入,函数调用是否正确。 另外,可能需要提醒用户安装统计工具箱,如果他们没有的话。或者提供两种方法:用iqr函数和手动计算。这样用户即使没有工具箱也能操作。 还要检查语法是否正确,比如quantile函数的参数是否正确,比如0.25和0.75的位置。确保示例代码运行不会出错。比如,数据示例可以举一个简单的,比如[3, 1, 4, 1, 5, 9, 2, 6],然后计算中值和IQR,并给出结果。这样用户可以直接测试。 可能还需要解释中值和IQR的意义,但用户的问题主要是如何用MATLAB,所以可能不需要深入解释统计概念,但简单提一下有助于理解。 最后,确保回答结构清晰,分步骤,代码用正确的格式,比如用代码块标出MATLAB代码,并给出注释。同时检查是否所有数学表达式都用了$...$,比如在解释IQR的时候,写成$Q3 - Q1$。不过用户的问题里没有数学公式,可能不需要太多数学表达式,但如果有的话要符合格式要。 总结步骤: 1. 计算中值:median函数。 2. 计算IQR: a. 使用iqr函数(需要统计工具箱)。 b. 或手动用quantile计算Q1和Q3,再相减。 3. 提供示例代码和结果。 4. 注意事项,比如工具箱的安装。 需要确保回答准确,步骤清晰,用户能跟着操作。可能还需要测试一下示例代码,确保正确性。比如,数据数组排序后是否正确计算中值和四分位数。例如,示例数据排序后是[1,1,2,3,4,5,6,9],中值是(3+4)/2=3.5,Q1是1.5,Q3是5.5,IQR是4。这样用户验证结果是否正确。</think>在MATLAB中一维数组中值(Median)和四分位距(IQR)可按以下步骤操作: --- ### **1. 计算中值(Median)** 使用内置函数 `median()`: ```matlab data = [3, 1, 4, 1, 5, 9, 2, 6]; % 示例数据 med = median(data); % 计算中值 ``` - **输出**:`med = 3.5`(数据排序后中间两个数的平均值:3和4) --- ### **2. 计算四分位距(IQR)** IQR是上四分位数(Q3)与下四分位数(Q1)之差,有两种实现方式: #### **方法1:直接使用 `iqr()` 函数** 需要安装**Statistics and Machine Learning Toolbox**: ```matlab iqr_value = iqr(data); % 直接计算IQR ``` #### **方法2:手动计算(无需工具箱)** 通过 `quantile()` 函数分步计算: ```matlab Q1 = quantile(data, 0.25); % 下四分位数(25%分位点) Q3 = quantile(data, 0.75); % 上四分位数(75%分位点) iqr_value = Q3 - Q1; % 计算IQR ``` - **输出**:`iqr_value = 4`(排序后数据的Q1=1.5,Q3=5.5,差值=5.5-1.5=4) --- ### **完整示例代码** ```matlab % 定义数据 data = [3, 1, 4, 1, 5, 9, 2, 6]; % 计算中值 med = median(data); disp(['中值 = ', num2str(med)]); % 计算IQR Q1 = quantile(data, 0.25); Q3 = quantile(data, 0.75); iqr_value = Q3 - Q1; disp(['IQR = ', num2str(iqr_value)]); ``` --- ### **注意事项** 1. **数据格式**:确保输入是一维数组(行向量或列向量)。 2. **分位数计算规则**:MATLAB默认使用线性插值法计算分位数,与其他工具(如Excel)可能略有差异。 3. **工具箱依赖**:`iqr()` 需要统计工具箱,若未安装,使用手动方法更可靠。 通过上述步骤,可快速解一维数据的中值和IQR。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值