简体   繁体   English

Octave for循环中意外的printf()行为

[英]Unexpected printf() behavior in Octave for-loop

I just recently switched from MATLAB to Octave because my license ran out and I wanted to give it a shot before renewing, so I can't check the behavior in MATLAB right now. 我最近刚从MATLAB切换到Octave,因为许可证用完了,我想在续约前先试一下,所以现在无法检查MATLAB中的行为。 I am not sure if I am making a grave mistake here, but I am having some headache over a for-loop in Octave. 我不确定在这里是否犯了一个严重的错误,但是我对Octave中的for循环感到有些头疼。

The loop was supposed to find the first "large" change ( 0.08 ) in the values of one of the columns (column 5 in this case) of a csv-file I previously read with dlmread() , and then return to me the index of the first change so I can discard all the data before this first big change occurs for my further computations. 该循环应该在我先前使用dlmread()读取的csv文件的某一列(在本例中为第5列)的值中找到第一个“大”变化( 0.08 dlmread() ,然后将索引返回给我的第一个变化,因此我可以在发生第一个大变化之前丢弃所有数据,以进行进一步的计算。

While trying to solve this I remembered that one should avoid for-loops in MATLAB (and probably Octave as well), and I managed to get the result I want by just doing 在尝试解决这一问题时,我记得一个人应该避免在MATLAB中使用for循环(可能也要避免使用Octave),并且我设法通过做来获得了想要的结果

idx = find(diff(gpsdata(:,5)) > 0.08, 1);

which is obviously much cleaner (and probably faster too?) and I will stick with it; 这显然更干净(也许也更快),我会坚持下去; however, I am still wondering why the for-loop is showing this weird behavior. 但是,我仍然想知道为什么for循环显示这种奇怪的行为。

Is there a mistake in my code, or is that just a sign of why for-loops should be avoided in Octave/MATLAB? 我的代码中是否有错误,还是仅是为什么在Octave / MATLAB中应避免for循环的标志? Are for-loops parallelized in Octave/MATLAB, is that why this is happening? 在Octave / MATLAB中将for循环并行化了,这就是为什么会这样吗?

Code

for (jj = 1:(size(gpsdata, 1) - 1))
  if (gpsdata(jj, 5) + 0.08 < (gpsdata(jj+1, 5)))
    idx = jj;
    disp(["jj=" num2str(jj) ", idx=" num2str(idx)]);
    printf("Found index %d, at %f s real time\n", num2str(idx), gpsdata(idx, 2));
    break;
  end
end

Actual Output: 实际输出:

jj=380, idx=380
Found index 51, at 56.000000 s real time
Found index 48, at 19.770000 s real time

Expected Output: 预期产量:

jj=380, idx=380
Found index 380, at 19.770000 s real time

The disp() call produced the correct output, while the printf() is somehow executed twice on top of returning wrong values, with only the fourth of the 4 arguments being correct (the 19.770000 is the expected output here); disp()调用产生了正确的输出,而printf()在返回错误值的基础上以某种方式执行了两次,只有四个参数中的第四个是正确的( 19.770000是这里的预期输出); the other 3 values ( 51 , 56.000000 , and 48 ) are wrong. 其他3个值( 5156.000000 ,和48 )是错误的。

Data for Reproducing 再现数据

The data I am using for gpsdata can be found here , and read in with data=load("~/gpsdata.mat"); 我用于gpsdata的数据可以在这里找到,并通过data=load("~/gpsdata.mat"); followed by gpsdata=data.gpsdata; 随后是gpsdata=data.gpsdata; .

As pointed out by @rahnema1, I was passing a string instead of an int by doing printf("%d", num2str(idx)) so Octave was printing the ascii codes for "380" , which happen to be 51 , 56 , 48 . 正如@ rahnema1指出的,我经过一个string而不是int通过做printf("%d", num2str(idx))所以倍频程中打印的ASCII码"380" ,这正好是515648

Doing printf("%d", idx) yields the expected behavior. 进行printf("%d", idx)产生预期的行为。

Thanks for catching that! 感谢您抓住这一点!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM