matlab迭代输出过程详解,Matlab - 如何输出for循环作为一个表,每次迭代和结果显示(Matlab - How to output a for loop as a table with e...

Matlab - 如何输出for循环作为一个表,每次迭代和结果显示(Matlab - How to output a for loop as a table with each iteration and result displayed)

我想知道如何在Matlab中输出for循环,以便最终得到一个表,其中第一列是迭代编号,第二列是每次迭代的结果。 我希望每次迭代的结果不仅显示最终答案。

作为一个非常简单的例子,我有下面的for循环函数。

谢谢。

p=10

for i=[1 2 3 4 5 6 7 8 9]

p=2*p

end

I am wondering how to output a for loop in Matlab so that I end up with a table where the first column is the iteration number and the second column is the result of each iteration. I want the results of each iteration to display not just the final answer.

As a very simple example I have the for loop function below.

Thanks.

p=10

for i=[1 2 3 4 5 6 7 8 9]

p=2*p

end

原文:https://stackoverflow.com/questions/46136477

2019-11-06 10:17

满意答案

在你的例子中, i是迭代变量,所以你可以引用它来获得迭代次数。

我假设你的意思是你想输出一个数组(而不是实际的表数据结构)。 要创建一个数组,你可以使用一些简单的连接:

p = 10;

arr = [];

for i = 1:9 % shortcutting your manual method here

arr = [arr; i p]; % concatenate the current array with the new row

p = p .* 2;

end

其结果是:

arr =

1 10

2 20

3 40

4 80

5 160

6 320

7 640

8 1280

9 2560

如果你真的想要一个表,那么你可以使用table函数从数组中创建table

In your example, i is the iteration variable, so you can reference it to get the iteration number.

I'm assuming you meant you want to output an array (not an actual table data structure). To make an array, you can use some simple concatenation:

p = 10;

arr = [];

for i = 1:9 % shortcutting your manual method here

arr = [arr; i p]; % concatenate the current array with the new row

p = p .* 2;

end

Which results in:

arr =

1 10

2 20

3 40

4 80

5 160

6 320

7 640

8 1280

9 2560

If you actually want a table, then you can create the table from the array using the table function

2017-09-10

相关问答

在你的例子中, i是迭代变量,所以你可以引用它来获得迭代次数。 我假设你的意思是你想输出一个数组(而不是实际的表数据结构)。 要创建一个数组,你可以使用一些简单的连接: p = 10;

arr = [];

for i = 1:9 % shortcutting your manual method here

arr = [arr; i p]; % concatenate the current array with the new row

p = p .* 2;

end

其结果是: a...

我最简单的解决方案是为每个file.names评估rbind一个数据帧,并将此行命名为相应文件的相同名称。 为清晰起见,我删除了评论(并提出了一些意见)。 'genAnnualized' = function(df_list) {

next.file

next.name

首先,这个问题是一个很好的例子,展示了如何从C ++与Matlab交互。 一般来说,我认为代码运行良好。 唯一的问题是每次迭代都会覆盖输出文件。 所以最好在循环之前打开文件,然后在循环之后关闭它。 或者使用append参数( ofstream myfile("outputmatlab.txt", ios_base::app) )打开文件,以防每次迭代都有理由打开文件。 我同意oro777在循环中使用engOpen()看起来不太好。 但是,由于仅在发动机指针(ep)为0时才打开发动机,因此发动机仅打...

问题在于你没有将实际的表名传递给你的连接字符串,你宁愿传递字面handle(i) 。 另一种方法是手动构建字符串或使用sprintf函数。 另外, handle是一个单元格,要访问它的内容,您应该使用{}而不是() 。 我也将计数器从i代入了matlab中的内置虚构单元到ii 。 handle=list;

size_h = max(size(handle));

for ii = 1:size_h

e = sprintf('create table %s (col int(11))',han...

我不知道“过程的结果”是什么意思,但是,你的问题的解决方案之一是 clear all

clc

D = dir('*.txt');

data = cell(length(D), 2);

for kk=1:length(D);

blockname = D(kk).name;

%process > result(kk);

data{kk,1}= kk; %

...

您正在使用==测试浮点数。 请注意, -1+120*.01==0.2为假,因为它们在浮点表示中不相等。 0.01*20==0.2恰好是真的。 相反,使用公差,例如, if abs(x-0.20)<1e-10 。 You are testing a floating point number with ==. Note that -1+120*.01==0.2 is false since they are not equal in the floating point representation...

因此,当我用以下代码替换if语句时,问题基本上消失了: if mod(i,1e4) == 0 % only for demonstration, not in final script

fprintf(1,'time for last 10000 iterations: %f \n',toc); tic;

end

我认为对toc的操作可能会导致问题 So, the problem gets largely eliminated when I replace the if statemen...

pause命令仅暂停Matlab执行。 由于Matlab执行的正常流程不是为了更新工作空间,因此在此实例中暂停不会执行您想要的操作。 keyboard命令使Matlab进入调试模式。 此模式将使用您当前的值更新工作区。 您可以通过在命令行中键入dbcont来继续执行。 另一种选择是在编辑器中打开.m文件,然后单击该行左侧行号旁边的“ - ”。 这也将进入调试模式。 在这种情况下,按F5键入dbcont或按播放按钮继续执行。 这里的内容是:调试模式是你的朋友。 以下是参考: 暂停 键盘 The pa...

相关文章

Data Week: Becoming a data scientist Data Pointed,

...

中文名: MATLAB及应用 作者: 胡鹤飞 图书分类: 软件 资源格式: PDF

...

As you know, I've been playing with Solr lately, tr

...

5th Jan, 10 Drupal drupal advanced forum drupa

...

In a time 有一次 Where the sun descends alone, 太阳孤独的降落

...

Gao Yuanyuan from after 2005 drama " world fir

...

在使用jsp生成web图片时遇到这个问题,这是源代码中的一条语句,源代码可以执行,可是一将源码放入ec

...

Spring Data: a new perspective of data operations

...

Windowsis an extremely effective and a an efficient

...

With an executive staffing venture about to open, a

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值