matlab中如何将循环数据一项一项保存到excel文件中

发布于 2024-11-24 11:26:49 字数 654 浏览 2 评论 0原文

我在 Excel 文件工作表 1 中有大量数据。列数是固定的 (6),但有很多行。

对于每 3 行,我需要选出第二列的最小值并将整行保存到 Excel 文件或工作表 2 中,我将如何编写脚本?

item.xls(sheet 1):

0.3 0.5 0.1 0.8 0.4 0.6
0.2 0.4 0.9 0.1 0.9 0.4
0.2 0.3 0.1 0.01 0.2 0.5
0.3 0.5 0.1 0.8 0.01 0.2
0.2 0.2 0.9 0.1 0.2 0.4
0.2 0.3 0.1 0.01 0.3 0.5
.......

前3行中,第2列的最小值为0.3,然后将整行写入Excel文件的sheet 2中。

然后接下来的3行,第2列的最小值是0.2,然后将整行写入Excel文件的第2张表中。

我想得到的结果是:

item.xls (sheet2):

0.2 0.3 0.1 0.01    % 1st 3 rows, the minimum value is 0.3 in 2nd column
0.2 0.2 0.9 0.1     % 2nd set of 3 rows, the minimum value is 0.2 in 2nd column
...

I have a huge set of data in Excel file sheet 1. The number of columns is fixed (6) but there are lots of rows.

For every 3 rows, I need to pick out the minimum value of 2nd column and save the whole row into Excel file or sheet 2, how am I going to write the script?

item.xls (sheet 1):

0.3 0.5 0.1 0.8 0.4 0.6
0.2 0.4 0.9 0.1 0.9 0.4
0.2 0.3 0.1 0.01 0.2 0.5
0.3 0.5 0.1 0.8 0.01 0.2
0.2 0.2 0.9 0.1 0.2 0.4
0.2 0.3 0.1 0.01 0.3 0.5
.......

In the first 3 rows, the minimum value of 2nd column is 0.3, then write the whole row into sheet 2 of the Excel file.

Then the next 3 rows, the minimum value of 2nd column is 0.2, then write the whole row into sheet 2 of the Excel file.

The result I would like to get is:

item.xls (sheet2):

0.2 0.3 0.1 0.01    % 1st 3 rows, the minimum value is 0.3 in 2nd column
0.2 0.2 0.9 0.1     % 2nd set of 3 rows, the minimum value is 0.2 in 2nd column
...

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

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

发布评论

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

评论(2

谁人与我共长歌 2024-12-01 11:29:28

下面,对您的问题进行一些澄清将有助于找到更好的解决方案。根据我对你的问题的解释,以下内容可能会有所帮助。我在这里生成随机测试数据。

% the number of test data rows
N = 12;

% generate some random vectors for testing
test1 = rand(N,1);
test2 = rand(N,1);
test3 = rand(N,1);
test4 = rand(N,1);

% create a temporary matrix to store the minimum of every
% 3 row set
final = zeros(N/3,4);

% loop in increments of 3
j = 1;
for k=1:3:N
    tmp = test2(k:k+2);

    % find the index of the minimum in this 3 row group of the 2nd col
    idx = find(tmp<=min(tmp));

    % offset idx to index into the original data properly
    idx = idx+k-1;

    % assign the "row" to the final variable
    final(j,:)=[test1(idx) test2(idx) test3(idx) test4(idx)];
    j = j+1;
end
% write the full results out at once
xlswrite('test.xls',final);

尝试一下,如果它不完全是您想要的,请发表评论以澄清您的问题。

Down, some clarification on your problem would help to find a better solution. Based on my interpretation of your question, the following bit may help. I'm generating random test data here.

% the number of test data rows
N = 12;

% generate some random vectors for testing
test1 = rand(N,1);
test2 = rand(N,1);
test3 = rand(N,1);
test4 = rand(N,1);

% create a temporary matrix to store the minimum of every
% 3 row set
final = zeros(N/3,4);

% loop in increments of 3
j = 1;
for k=1:3:N
    tmp = test2(k:k+2);

    % find the index of the minimum in this 3 row group of the 2nd col
    idx = find(tmp<=min(tmp));

    % offset idx to index into the original data properly
    idx = idx+k-1;

    % assign the "row" to the final variable
    final(j,:)=[test1(idx) test2(idx) test3(idx) test4(idx)];
    j = j+1;
end
% write the full results out at once
xlswrite('test.xls',final);

Try this out and if it's not quite what you are looking for, post a comment to clarify your question.

同展鸳鸯锦 2024-12-01 11:28:35

我将展示如何以矢量化方式从数据中提取相关行。我将把读取/写入 Excel 文件的部分留给你(它应该很简单):

%# sample data
data = rand(3*10,6);

%# for each three rows, find the min of second column, and get the row index
[r c] = size(data);
d = permute(reshape(data',[c 3 r/3]),[2 1 3]);  %'# split into third dimension
[~,idx] = min(d(:,2,:));                        %# find min of col2
idx = squeeze(idx) + (0:3:(r-1))';              %'# adjust indices

%# extract these rows from the data matrix
result = data(idx,:);

I will show how to extract the relevant rows from the data in a vectorized manner. I will leave the part of reading/writing excel files to you (it should be straightforward):

%# sample data
data = rand(3*10,6);

%# for each three rows, find the min of second column, and get the row index
[r c] = size(data);
d = permute(reshape(data',[c 3 r/3]),[2 1 3]);  %'# split into third dimension
[~,idx] = min(d(:,2,:));                        %# find min of col2
idx = squeeze(idx) + (0:3:(r-1))';              %'# adjust indices

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