错误:尝试访问数据(2,:);索引越界,因为 size(data)=[1,7129]

发布于 2024-09-01 03:03:50 字数 462 浏览 5 评论 0原文

我有一个38行7130列的微阵列数据。我正在尝试读取数据,但仍然出现上述错误。

我调试了一下,读取数据时发现,我的是1x7129,而不是38x7130。我不知道为什么。我的第 7130 列包含字母,其余数据是数字。知道为什么会发生这种情况吗?


我的文件位于文本制表符分隔中,这是我用于读取文件的代码:

clear; 
fn=32; 
col=fn+1; 
cluster=2; 
num_eachClass=3564; 
row=num_eachClass*cluster; 
fid1 = fopen('data.txt', 'r'); 
txt_format=''; 
for t=1:col txt_format=[txt_format '%g ']; 
end 
data = fscanf(fid1,txt_format,[col row]); 
data = data'; fclose(fid1); 

I have a micro-array data of 38 row and 7130 columns. I am trying to read the data but keeping having the above error.

I debugged and found when I read the data, I have a 1x7129 instead of a 38x7130. I don't know why. My 7130th column contains letters while the rest of the data are numbers. Any idea why this is happening?


My file is in text tab delimited and here is my code for reading the file:

clear; 
fn=32; 
col=fn+1; 
cluster=2; 
num_eachClass=3564; 
row=num_eachClass*cluster; 
fid1 = fopen('data.txt', 'r'); 
txt_format=''; 
for t=1:col txt_format=[txt_format '%g ']; 
end 
data = fscanf(fid1,txt_format,[col row]); 
data = data'; fclose(fid1); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-09-08 03:03:50

尝试使用此代码来读取数据:

filename = 'yourfilename.txt';
fid = fopen(filename,'r');

% If you have a line with column headers use those 3 lines. Comment if not.
colnames = fgetl(fid);
colnames = textscan(colnames, '%s','delimiter','\t');
colnames = colnames{:};

% Reading the data
tsformat = [repmat('%f ',1,7129) '%s'];
datafromfile = textscan(fid,tsformat,'delimiter','\t','CollectOutput',1);
fclose(fid);

% Get the data from the cell array    
data = datafromfile{1};
labels = datafromfile{2};

编辑
要将数据集分开进行训练和测试,请执行以下操作:

train_samp = 1:19;
test_samp = 20:38;
train_data = data(train_samp,:);
test_data = data(test_samp,:);
train_label = labels(train_samp);
test_label = labels(test_samp);

您还可以随机分离样本:

samp_num = size(data,1);
test_num = 19;
randorder = randperm(samp_num);
train_samp = randorder(test_num+1:samp_num);
test_samp = randorder(1:test_num);

我还没有进行转置data = data';
如果必须的话,只需在上面的代码中切换行和列索引:

train_data = data(:,train_samp);
test_data = data(:,test_samp);

Try this code to read the data:

filename = 'yourfilename.txt';
fid = fopen(filename,'r');

% If you have a line with column headers use those 3 lines. Comment if not.
colnames = fgetl(fid);
colnames = textscan(colnames, '%s','delimiter','\t');
colnames = colnames{:};

% Reading the data
tsformat = [repmat('%f ',1,7129) '%s'];
datafromfile = textscan(fid,tsformat,'delimiter','\t','CollectOutput',1);
fclose(fid);

% Get the data from the cell array    
data = datafromfile{1};
labels = datafromfile{2};

EDIT
To separate your dataset to training and test, do something like this:

train_samp = 1:19;
test_samp = 20:38;
train_data = data(train_samp,:);
test_data = data(test_samp,:);
train_label = labels(train_samp);
test_label = labels(test_samp);

You can also separate samples randomly:

samp_num = size(data,1);
test_num = 19;
randorder = randperm(samp_num);
train_samp = randorder(test_num+1:samp_num);
test_samp = randorder(1:test_num);

I haven't done transposition data = data';.
If you have to, just switch row and column indexes in the above code:

train_data = data(:,train_samp);
test_data = data(:,test_samp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文