用数字和字符串创建矩阵/数组

时间:2014-07-21 14:52:32

标签: arrays matlab matrix

我正在使用matlab。我有一个函数,目前返回5个数组,但我想将数组加入单个矩阵或只是一个单元格数组,每个数组的标题是当前函数的输出?

例如,输出如下:

low_sec    lowmid_sec
1              7  
2              6    
35             5
5              43

有什么想法吗?

function [low_sec ,lowmid_sec , middle_sec , upmid_sec , upper_sec]=     sepfunc(intensdata)lengthofdata=length(intensdata); 
count1=0;
count_2=0;
count_3=0;
count_4=0;
count_5=0;

 for i=  1:lengthofdata %loop to seperate count number of data in 5 groups 
    if (intensdata(i,1)<0.05)
        count1=count1+1;     
    elseif (intensdata(i,1)>=0.05 && intensdata(i,1)<0.1)
        count_2=count_2+1;
    elseif (0.1<=intensdata(i,1) && intensdata(i,1)<0.15)
        count_3=count_3+1;
    elseif (0.15<=intensdata(i,1) && intensdata(i,1)<0.2)
        count_4=count_4+1;
    elseif (intensdata(i,1)>=0.2 )
        count_5=count_5+1;
    end
 end
  disp(count1);
  disp(count_2);
  disp(count_3);
  disp(count_4);
  disp(count_5);
   j=1;
   k=1;
   m=1;
   n=1;
   x=1; 
   low_sec=[count1];
   lowmid_sec=[count_2];
   middle_sec=[count_3];
   upmid_sec=[count_4];
   upper_sec=[count_5]; 


for i=  1:lengthofdata %to seperate original data into 5 different sub-groups. 
   if (intensdata(i,1)<0.05)
        low_sec(j,1)=intensdata(i,1);
        j=j+1 ;
    elseif(0.05<=intensdata(i,1) && intensdata(i,1)<0.1)       
        lowmid_sec(k,1)=intensdata(i,1);
        k=k+1;
    elseif(0.1<=intensdata(i,1) && intensdata(i,1)<0.15)       
        middle_sec(m,1)=intensdata(i,1);
        m=m+1;
    elseif(0.15<=intensdata(i,1) && intensdata(i,1)<0.2)       
        upmid_sec(n,1)=intensdata(i,1);
        n=n+1;
    elseif( intensdata(i,1)>=0.2)       
        upper_sec(x,1)=intensdata(i,1);
        x=x+1; 
   end

end

1 个答案:

答案 0 :(得分:4)

您有几个选项,如您所提到的单元数组,使用新表结构或最简单的方法是创建结构。

为此,您只需在功能结束时添加以下内容:

sec.low = low_sec;
sec.lowmid = lowmid_sec;
sec.middle = middle_sec;
sec.upmid = upmid_sec;
sec.upper = upper_sec;

然后将第一行更改为:

function sec = sepfunc(intensdata)