ずっと悩んでいます。どうかご教授ください。
現在test.txtにはいかのようなものが添付されています。
1.0000000e+000 2.0000000e+000 3.0000000e+000
2.0000000e+000 3.0000000e+000 4.0000000e+000
3.0000000e+000 4.0000000e+000 5.0000000e+000
4.0000000e+000 5.0000000e+000 6.0000000e+000
5.0000000e+000 6.0000000e+000 7.0000000e+000
6.0000000e+000 7.0000000e+000 8.0000000e+000
7.0000000e+000 8.0000000e+000 9.0000000e+000
8.0000000e+000 9.0000000e+000 1.0000000e+001
9.0000000e+000 1.0000000e+001 1.1000000e+001
1.0000000e+001 1.1000000e+001 1.2000000e+001
1.1000000e+001 1.2000000e+001 1.3000000e+001
1.2000000e+001 1.3000000e+001 1.4000000e+001
1.3000000e+001 1.4000000e+001 1.5000000e+001
1.4000000e+001 1.5000000e+001 1.6000000e+001
1.5000000e+001 1.6000000e+001 1.7000000e+001
1.6000000e+001 1.7000000e+001 1.8000000e+001
1.7000000e+001 1.8000000e+001 1.9000000e+001
1.8000000e+001 1.9000000e+001 2.0000000e+001
ここから縦10行を一組としてカウントの回数だけ1つづつずらしたいのですが
0が入ってしまいます。私はこの0となっている場所にしっかりとした値、
この場合ですと(例:サンプル11に11 12 13)を入れたいです。何日か考えてみたので
すが解決できません。
どうかご教授ください。以下に現在のソースを示します。
#include stdafx.h
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//test.txtには1サンプルにつきtx(最初の2列)ty(次の1列)のように値が並べられて
いる。
/*サンプル数(データ数)*/
#define NUM_SAMPLE 98
//区切る間隔
#define NUM_test 10
#define NUM_INPUT 2
#define NUM_OUTPUT 1
double tx[NUM_SAMPLE][NUM_INPUT] ;
double ty[NUM_SAMPLE][NUM_OUTPUT];
double t[NUM_SAMPLE][NUM_SAMPLE];
int main(void)
{
int isample, i, j;
FILE *stream;
FILE *fp1;
char fname1[10];
sprintf(fname1,testdata.txt);
if((fp1=fopen(fname1,w+))==NULL){
printf(cannot open file1.);
return 0;
}
/*データファイルの読み込み*/
stream = fopen( test.txt, r);//横に
if (stream == NULL)
{
printf(ファイルdata1.txtをオープンできません.\n);
exit(0);
}
else
{
/*ファイルからデータを読み込む*/
int cc=0;
for(int count=0 ; count<3; count++) /*MAXの訓練データまで幾度
か学習を行う*/
{
cc=cc+1;
/*ファイルからデータを読み込む*/
for ( isample=cc; isample <NUM_test+cc; isample=isample+1){
for ( i = 0; i < NUM_INPUT; i++)
{
fscanf( stream, %lf, &tx[isample][i]);
}
for ( j = 0; j < NUM_OUTPUT; j++)
{
fscanf( stream, %lf, &ty[isample][j]);
}
}
/*読み込んだデータの表示*/
for ( isample=cc; isample <NUM_test+cc; isample=isample+1)
{
printf(サンプル数%d,入力:,isample);
for ( i = 0; i < NUM_INPUT; i++)
{
printf( %lf ,tx[isample][i]);
}
printf(出力: );
for ( j = 0; j < NUM_OUTPUT; j++)
{
printf( %lf\n , ty[isample][j]);
}
printf(\n);
}
fclose( stream);
}
}
return 0;
}
こんなかんじでどうでしょう。
/*ファイルからデータを読み込む*/
for( isample = 0; isample < NUM_SAMPLE; ++isample){
if(feof( stream ))
break;
for ( i = 0; i < NUM_INPUT; i++) {
if( !fscanf( stream, %lf, &tx[isample][i]) ){
fprintf(stderr, err : %d, isample);
break;
}
}
for ( j = 0; j < NUM_OUTPUT; j++){
if( !fscanf( stream, %lf, &ty[isample][j]) ){
fprintf(stderr, err : %d, isample);
break;
}
}
}
for(int count=0 ; count<3; count++) {
for( isample = 0; isample < NUM_test; ++isample){
printf(サンプル数%02d,入力:,isample);
for ( i = 0; i < NUM_INPUT; i++) {
printf( %lf ,tx[isample + count][i]);
}
printf(出力: );
for ( j = 0; j < NUM_OUTPUT; j++) {
printf( %lf\n, ty[isample + count][i]);
}
}
}
fclose( stream);
有難うございます。
出来ました。
なかなかこのような発想が思いつきませんでした。
参考になりました。
お世話になりました。