全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:4157
推到 Plurk!
推到 Facebook!

請問如何實現抽樣及把大量資料塞入陣列

答題得分者是:jow
lulala
一般會員


發表:5
回覆:6
積分:2
註冊:2005-01-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-10-21 11:26:51 IP:75.185.xxx.xxx 訂閱
近日,我需要模擬大量資料,原本用的軟體已經不敷使用,且因為該資料龐大,所以十分要求效率,
很不幸的是我對BCB是超級新手~

查閱站上文章,我知道有許多讀入TXT檔的方法,但對我來說,感覺都不是十分完整,實在是不得其門而入!!
希望各位前輩能幫忙一下!

資料檔案TEST.TXT
資料格式為
ALL,ALL,ALL,ALL,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML
-76,-34,-95,-12,-21,-202,-112,-118,-90,-137,-157,-172,-47,-62,-58,-161,-48,-176
-98,-144,-118,-172,-13,-274,-185,-142,-87,-51,-370,-122,-442,-198,-217,-215,-531,-284
-153,-17,59,12,8,59,24,212,102,-82,-77,38,-21,-5,63,-46,-124,-81
237,152,270,172,38,309,170,314,319,178,340,31,396,141,95,146,431,9
-215,-174,-229,-137,-128,-456,-197,-401,-283,-135,-438,-201,-351,-256,-191,-172,-496,-294
-122,-289,-383,-205,-245,-581,-400,-452,-385,-320,-364,-226,-394,-206,-230,-596,-696,-493
-68,361,172,358,409,-159,-215,-336,-726,-13,-216,242,236,-298,-86,-122,-1038,-393
-118,-89,-187,-104,-102,-343,-227,-310,-271,-11,-210,-117,-39,-218,-152,-341,-441,-141
6,87,185,-25,85,236,100,177,-12,112,-86,-6,95,-14,-6,171,235,166
208,104,157,147,281,-7,307,-131,-104,-176,253,179,203,100,-249,-147,157,-37
-25,-239,66,-221,-217,-542,48,-151,4,-3,-188,34,-75,-125,-117,-533,-162,-39
-430,-436,-525,-297,-469,-744,-677,-636,-607,-516,-238,-517,-507,-688,-443,-1185,-1453,-859
-348,-349,-401,-289,-170,-331,-504,-1166,-433,-373,-531,-101,-292,-399,-252,-789,-1013,-572
-79,-197,-162,-81,14,-372,-98,-157,-79,-143,-188,-128,-416,-212,-221,-547,-317,-150
這邊舉例為18*15矩陣,但實際資料量為約20000*150的矩陣
我已經寫完演算法的部分,丟進演算法的資料也是TXT,格式是一樣的
但是困難的地方在於,丟進演算法的資料是抽樣的,抽出的樣本為Columnwise
抽樣的方法我已經有了,存於另一個LIST中
比如說list.txt為
1,4,6,9,13,7,3

因此我要把第1,3,4,6,7,9,13 column抓出來做成TEST.TXT丟給我的演算法
資料如下
ALL,ALL,ALL,AML,AML,AML,AML
-76,-95,-12,-202,-112,-90,-47
-98,-118,-172,-274,-185,-87,-442
-153,59,12,59,24,102,-21
237,270,172,309,170,319,396
-215,-229,-137,-456,-197,-283,-351
-122,-383,-205,-581,-400,-385,-394
-68,172,358,-159,-215,-726,236
-118,-187,-104,-343,-227,-271,-39
6,185,-25,236,100,-12,95
208,157,147,-7,307,-104,203
-25,66,-221,-542,48,4,-75
-430,-525,-297,-744,-677,-607,-507
-348,-401,-289,-331,-504,-433,-292
-79,-162,-81,-372,-98,-79,-416


請問這樣的需求要如何在BCB實現呢?
順便請問一下,BCB理有辦法抽樣嗎?
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-10-21 17:12:02 IP:123.193.xxx.xxx 訂閱
利用資料封裝物件(TRawDataFile)來分離並存取
自文字檔輸入的資料.

程式碼僅供參考, 請依個人需要修改使用...^_^

測試碼下載:
http://delphi.ktop.com.tw/download.php?download=upload/471b18de616b2_TestC014.zip

[code cpp]
//---------------------------------------------------------------------------
#ifndef fMainH
#define fMainH
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp><br />//---------------------------------------------------------------------------
class TRawDataFile : public TPersistent
{
protected:
TFileStream *F;
int FDataCount;
int FFieldCount;
void __fastcall InitialInternalFile();
int __fastcall CountingFields(AnsiString S);
int __fastcall GetData(int Index, int FieldNo);
public:
__fastcall TRawDataFile(AnsiString FileName);
__fastcall ~TRawDataFile(void);
void __fastcall LoadFromFile(AnsiString FileName);
__property int DataCount = {read=FDataCount};
__property int FieldCount = {read=FFieldCount};
__property int Data[int Index][int FieldNo] = {read=GetData};
};
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TListBox *ListBox1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
[/code]


[code cpp]
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "fMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TRawDataFile::TRawDataFile(AnsiString FileName)
: TPersistent()
{
InitialInternalFile();//default File, make F<>NULL
LoadFromFile(FileName);
}
//---------------------------------------------------------------------------
__fastcall TRawDataFile::~TRawDataFile(void)
{
delete F;
}
//---------------------------------------------------------------------------
void __fastcall TRawDataFile::InitialInternalFile()
{
if(F) delete F;//Close previous open file
F = new TFileStream("TEMP.DAT", fmCreate);
FDataCount = 0;
FFieldCount = 0;
}
//---------------------------------------------------------------------------
int __fastcall TRawDataFile::GetData(int Index, int FieldNo)
{
int ret = -MaxInt;//Error Data
if(Index>-1&&Index-1&&FieldNo int FieldSize = sizeof(int);
int RecSize = FieldSize * FFieldCount;
F->Position = RecSize * Index FieldSize * FieldNo;
F->Read(&ret, FieldSize);
}
return ret;
}
//---------------------------------------------------------------------------
int __fastcall TRawDataFile::CountingFields(AnsiString S)
{
int ret = 0;
S = S.Trim();
if(S!=""){
if(S[S.Length()]!=',') S = S ",";
for(int i=1; i<=S.Length(); i )
if(S[i]==',') ret ;
}
return ret;
}
//---------------------------------------------------------------------------
void __fastcall TRawDataFile::LoadFromFile(AnsiString FileName)
{
if(FileExists(FileName)){
InitialInternalFile();
TStringList *M = new TStringList();
try{
TStringList *L = new TStringList();
try{
L->LoadFromFile(FileName);
if(L->Count > 0){ //檔案至少有一筆非數值的資料,
//Counting and Adjust Input Data----------------------------------
FDataCount = L->Count-1; //扣除第一筆非數值的資料.
FFieldCount = CountingFields(L->Strings[0]); //由第一筆資料計算 FieldCount.
L->Delete(0); //將第 一筆資料移除.
//----------------------------------------------------------------
M->CommaText = L->Text;
}
}
__finally{
delete L;
}


for(int i=0; iCount; i ){
int V = StrToInt(M->Strings[i].Trim());
F->Write(&V, sizeof(V));

}
}
__finally{
delete M;
}
}
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1->Clear();
TRawDataFile *F = new TRawDataFile("TEST.TXT");
try{
ListBox1->Items->Add("DataCount=" IntToStr(F->DataCount));
ListBox1->Items->Add("FieldCount=" IntToStr(F->FieldCount));
ListBox1->Items->Add("");


AnsiString S;
for(int i=0; iDataCount; i ){
S.sprintf("Data[%d][%d]=%d", i,0,F->Data[i][0]);
ListBox1->Items->Add(S);

}
}
__finally{
delete F;
}
}
//---------------------------------------------------------------------------
[/code]
編輯記錄
jow 重新編輯於 2007-10-21 17:17:09, 註解 無‧
jow 重新編輯於 2007-10-22 07:36:10, 註解 無‧
jow 重新編輯於 2007-10-22 07:40:36, 註解 無‧
lulala
一般會員


發表:5
回覆:6
積分:2
註冊:2005-01-23

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-10-23 12:52:49 IP:75.185.xxx.xxx 訂閱
謝謝jow前輩!
經我仔細拜讀您的程式,由於我接觸BCB時間太淺了,所以您的程式我有許多地方不懂!(因您的程式含C 的封裝、繼承等概念,而我又太菜了,只會C)
而且完成的程式,必須PORT到AIX,SUN及LINUX中,所以必須為CONSOLE模式!
可否煩請前輩用純C為我解惑!
我對於此程式的概念為先將第一列取出(i=0),存到另一檔案
再將下一列(i=1)讀出,並一個個存到index 為j(j=1...k)的矩陣,
最後在LOOP i (i=1..n)
因此最後得到的是k個 vector(每個vector長度為n)
再合併第一列(i=0)的資料,成為k個長度為n 1的vector
之前曾試寫過此程式,但只能完全讀出所有檔案,並無法分列及分攔讀出!
因為我的每個欄位的VALUE都不是固定的,有正也有負的。
我不知如何按CHAR的方法正確讀出??
因為我搞這個已經好幾天了,想趕緊模擬我的資料,希望前輩能多多幫忙,謝謝。
編輯記錄
lulala 重新編輯於 2007-10-23 12:56:43, 註解 無‧
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-10-23 22:36:51 IP:123.193.xxx.xxx 訂閱


資料檔案TEST.TXT
資料格式為
ALL,ALL,ALL,ALL,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML,AML
-76,-34,-95,-12,-21,-202,-112,-118,-90,-137,-157,-172,-47,-62,-58,-161,-48,-176
-98,-144,-118,-172,-13,-274,-185,-142,-87,-51,-370,-122,-442,-198,-217,-215,-531,-284
-153,-17,59,12,8,59,24,212,102,-82,-77,38,-21,-5,63,-46,-124,-81
237,152,270,172,38,309,170,314,319,178,340,31,396,141,95,146,431,9
-215,-174,-229,-137,-128,-456,-197,-401,-283,-135,-438,-201,-351,-256,-191,-172,-496,-294
-122,-289,-383,-205,-245,-581,-400,-452,-385,-320,-364,-226,-394,-206,-230,-596,-696,-493
-68,361,172,358,409,-159,-215,-336,-726,-13,-216,242,236,-298,-86,-122,-1038,-393
-118,-89,-187,-104,-102,-343,-227,-310,-271,-11,-210,-117,-39,-218,-152,-341,-441,-141
6,87,185,-25,85,236,100,177,-12,112,-86,-6,95,-14,-6,171,235,166
208,104,157,147,281,-7,307,-131,-104,-176,253,179,203,100,-249,-147,157,-37
-25,-239,66,-221,-217,-542,48,-151,4,-3,-188,34,-75,-125,-117,-533,-162,-39
-430,-436,-525,-297,-469,-744,-677,-636,-607,-516,-238,-517,-507,-688,-443,-1185,-1453,-859
-348,-349,-401,-289,-170,-331,-504,-1166,-433,-373,-531,-101,-292,-399,-252,-789,-1013,-572
-79,-197,-162,-81,14,-372,-98,-157,-79,-143,-188,-128,-416,-212,-221,-547,-317,-150


測試碼下載
http://delphi.ktop.com.tw/board.php?cid=31&fid=130&tid=90953

[code cpp]
//---------------------------------------------------------------------------
#ifndef fRawDataFileH
#define fRawDataFileH
#include "stdio.h"
//---------------------------------------------------------------------------
class TRawDataFile
{
protected:
FILE* F;
int FDataCount;
int FFieldCount;
int CountFields(char* s);
void Init();
void Load(char* filename);
public:
TRawDataFile(char* filename){
Init();
Load(filename);
};
~TRawDataFile(void){if(F!=NULL)fclose(F);};
int Data(int Index, int FieldNo);
int DataCount(){return FDataCount;};
int FieldCount(){return FFieldCount;};
};
//---------------------------------------------------------------------------
#endif
[/code]

[code cpp]
//---------------------------------------------------------------------------
#pragma hdrstop
#include "fRawDataFile.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
void TRawDataFile::Init()
{
if(F!=NULL)fclose(F);//Close previous open file
F=fopen("TEMP.DAT","wb r");
FDataCount=0;
FFieldCount=0;
}
//---------------------------------------------------------------------------
int TRawDataFile::CountFields(char* s)
{
int ret=0;
int len=strlen(s);
char *p=new char[len 1];
memset(p,0,len 1);
memcpy(p,s,len);
if(p[len]!=',')p[len ]=',';
while(*p !=NULL)
if(*p==',')ret ;
return ret;
}
//---------------------------------------------------------------------------
void TRawDataFile::Load(char* filename)
{
FILE* fh;
int i, RecSize, size=256;
int *Rec;
char *token;
char *s=new char[size];
Init();
if((fh=fopen(filename,"rt"))!=NULL){
memset(s,0,size);
if(fgets(s,size,fh)!=NULL){
if((FFieldCount=CountFields(s))>0){
Rec=new int[FFieldCount];
RecSize=sizeof(int)*FFieldCount;
while(fgets(s,size,fh)!=NULL){
i=0;
token=strtok(s,",");
memset(Rec,0,RecSize);
while(i Rec[i ]=atoi(token);
token=strtok(NULL,",");
}
fwrite(Rec,RecSize,1,F);//write to TEMP.DAT
FDataCount ;
}
delete Rec;
}
}
fclose(fh);
}
}
//---------------------------------------------------------------------------
int TRawDataFile::Data(int Index, int FieldNo)
{
int ret=0x7FFFFFFF;//Error Data
if(F&&Index>-1&&Index FieldNo>-1&&FieldNo {
int FieldSize = sizeof(int);
int RecSize = FieldSize * FFieldCount;
fseek(F,RecSize * Index FieldSize * FieldNo,0);
fread(&ret,FieldSize,1,F);
}
return ret;
}
//---------------------------------------------------------------------------
[/code]

[code cpp]
//---------------------------------------------------------------------------
#pragma hdrstop
#include "fRawDataFile.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
//---------------------------------------------------------------------------
#pragma argsused
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
char *filename = "TEST.TXT", *s;
TRawDataFile *f=new TRawDataFile(filename);
if(f){
printf("FieldCount=%d\n",f->FieldCount());
printf("DataCount=%d\n",f->DataCount());
for(int i=0;iDataCount();i )
printf("Data[%d][%d]=%d\n",i,0,f->Data(i,0));
delete f;
}
system("pause");
return 0;
}
//---------------------------------------------------------------------------
[/code]
編輯記錄
jow 重新編輯於 2007-10-23 22:39:33, 註解 無‧
jow 重新編輯於 2007-10-23 22:51:56, 註解 無‧
jow 重新編輯於 2007-10-23 23:07:10, 註解 無‧
jow 重新編輯於 2007-10-24 00:34:59, 註解 無‧
lulala
一般會員


發表:5
回覆:6
積分:2
註冊:2005-01-23

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-10-24 20:07:13 IP:75.185.xxx.xxx 訂閱
謝謝前輩的幫忙!
系統時間:2024-11-23 17:31:20
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!