Re: ? grouping data in MatLab
※ 引述《ccos@kkcity.com.tw ( )》之銘言:
> Hi:
> I need to deal data using MatLab and the data is as follows.
> Data has: x, y, z, Type, and Val arrays, the number of data is: NoData
> The content of Type is 1 or 2. I need to do some process ONLY for those
> data whose Type == 1, and then do some process for those data whose Type
> == 2. More sepcifically,
> % Loop-1
> for n = 1st data whose Type == 1 to the last data whose Type == 1
> some processing
> end
> % Loop-2
> for n = 1st data whose Type == 2 to the last data whose Type == 2
> some processing
> end
> How do I do this in MatLab efficiently? Note, it is NOT allowed to do any
> processing in Loop-2 before those processing in Loop-1 are completed.
> The easiest approach is:
> for n = 1:1:NoData
> if Type(n) == 1
> do processing type-1
> end
> end % n loop
> for n = 1:1:NoData
> if Type(n) == 2
> do processing type-2
> end
> end % n loop
> But this is not very efficient, especially NoData is about 10^6 to 10^7.
> Thanks,
> by Cheng Cosine
> Jun/30/2k6 NC
You may use "vectorization" techniques rather than the traditional for-loop.
For exmaple, suppose your data format is defined as follows:
data = [x(1) y(1) z(1) type; x(2) y(2) z(2) type; ...;x(NoData) y(NoData) z(NoData) type];
ind = (data(:,4)==1); % find index of type 1 data
data(:,ind); % it represents type 1 data
ind = (data(:,4)==2); % find index of type 2 data
data(:,ind); % it represents type 2 data
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: thccy11.oz.nthu.edu.tw
討論串 (同標題文章)
Programming 近期熱門文章
PTT數位生活區 即時熱門文章