MATLAB - 索引超出矩阵维度

发布于 2024-09-03 21:29:15 字数 230 浏览 6 评论 0原文

您好,我有矩阵问题。

我有许多行数不同但列数相同(1 列)的 .txt 文件,

e.g. s1.txt = 1234 rows
     s2.txt = 1200 rows
     s2.txt = 1100 rows

我想合并这三个文件。由于它有不同的行..当我将其写入新文件时,我收到此错误=索引超出矩阵维度。

我该如何解决这个问题? 。

Hi I have problem with matrix..

I have many .txt files with different number of rows but have the same number of column (1 column)

e.g. s1.txt = 1234 rows
     s2.txt = 1200 rows
     s2.txt = 1100 rows

I wanted to combine the three files. Since its have different rows .. when I write it to a new file I got this error = Index exceeds matrix dimensions.

How I can solved this problem? .

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

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

发布评论

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

评论(3

姜生凉生 2024-09-10 21:29:15

您可以简单地通过堆叠来组合三个矩阵:假设 s1 等是您读入的矩阵,您可以像这样创建一个新矩阵:

snew = [s1; s2; s3];

如果您只需要执行一次,您还可以使用 [] 样式堆叠而不创建新的矩阵变量。

You can combine three matrices simply by stacking them: Assuming that s1, etc are the matrices you read in, you can make a new one like this:

snew = [s1; s2; s3];

You could also use the [] style stacking without creating the new matrix variable if you only need to do it once.

只等公子 2024-09-10 21:29:15

您提供的信息太少,无法准确诊断您的问题。也许您已将文件中的数据加载到工作区中的变量中。也许 s1 有 1 列和 1234 行,等等。然后您可以将变量连接成一个列向量,如下所示:

totalVector = [s1; s2; s3];

并使用 save() 将其写入文件陈述。

这有帮助吗?

You have provided far too little information for an accurate diagnosis of your problem. Perhaps you have loaded the data from your files into variables in your workspace. Perhaps s1 has 1 column and 1234 rows, etc. Then you can concatenate the variables into one column vector like this:

totalVector = [s1; s2; s3];

and write it out to a file with a save() statement.

Does that help ?

巨坚强 2024-09-10 21:29:15

让我假设这个问题与您的另一个 问题有关,并且您希望按列组合这些矩阵,在数据较少的列中留下空值。

在这种情况下,此代码应该可以工作:

BaseFile ='s';
n=3;
A = cell(1,n);
for k=1:n
    A{k} = dlmread([BaseFile num2str(k) '.txt']);
end

% create cell array with maximum number of rows and n number of columns
B = cell(max(cellfun(@numel,A)),n); 

% convert each matrix in A to cell array and store in B
for k=1:n
    B(1:numel(A{k}),k) = num2cell(A{k});
end

% save the data
xlswrite('output.txt',B)

该代码假设每个文件中都有一列,否则它将无法工作。

Let me make an assumption that this question is connecting with your another question, and you want to combine those matrices by columns, leaving empty values in columns with fewer data.

In this case this code should work:

BaseFile ='s';
n=3;
A = cell(1,n);
for k=1:n
    A{k} = dlmread([BaseFile num2str(k) '.txt']);
end

% create cell array with maximum number of rows and n number of columns
B = cell(max(cellfun(@numel,A)),n); 

% convert each matrix in A to cell array and store in B
for k=1:n
    B(1:numel(A{k}),k) = num2cell(A{k});
end

% save the data
xlswrite('output.txt',B)

The code assumes you have one column in each file, otherwise it will not work.

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