问题描述
这是我的代码的一部分.
This is a part of my code.
a = 0;
for i = -3:3
for j = 1:10
a(j) = j^i;
end
xlswrite('out.xls', a','A1')
end
在这里,我仅获得out.xlx
文件中的a
的最后一个值.但是我想在Excel
文件的连续列中为i
的每次迭代填充a
的值.有什么建议 ?更新 我想要这样的输出:-
Here I am getting only the last value of a
in the out.xlx
file. But I want to fill the values of a
for each iteration of i
in consecutive columns of an Excel
file . Any suggestions ?Update I want output like this: -
1.0000 1.0000 1.0000 1 .....
0.1250 0.2500 0.5000 1 .....
0.0370 0.1111 0.3333 1 .....
0.0156 0.0625 0.2500 1 .....
0.0080 0.0400 0.2000 1 .....
0.0046 0.0278 0.1667 1 .....
0.0029 0.0204 0.1429 1 .....
0.0020 0.0156 0.1250 1 .....
0.0014 0.0123 0.1111 1 .....
0.0010 0.0100 0.1000 1 .....
以此类推....
推荐答案
您已经生成了1D向量而不是2D矩阵.这应该起作用:
You have produced a 1D vector instead of a 2D matrix. This should work:
a = 0;
for i = -3:3
for j = 1:10
a(j,i+4) = j^i;
end
xlswrite('out.xls', a',['A' num2str(i+4) ':J' num2str(i+4)])
end
更好的解决方案是对所有内容进行矢量化处理
An even better solution would be to vectorize everything:
[X,Y] = meshgrid(-3:3,1:10)
a = X.^Y
xlswrite('out.xls', a','A1:J7')
@ EitanT,@ HighPerformanceMark:更改了答案以反映您的评论.我想今天不是我的日子-编辑了4次:)
@EitanT, @HighPerformanceMark: Changed the answer to reflect your comment. I guess today is not my day - edited 4 times :)
这篇关于将数据数组从循环写入Excel文件的连续列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!