MATLAB中未知长度的矩阵? [英] Matrix of unknown length in MATLAB?

查看:45
本文介绍了MATLAB中未知长度的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个长度可变的零矩阵,其中包含两列,我可以将 while 循环的结果输出到其中(目的是使用它来存储欧拉方法中的步骤数据,并调整时间步长).长度由循环的迭代次数决定.

I'm trying to set up a zero matrix of variable length with two columns into which I can output the results of a while loop (with the intention of using it to store the step data from Euler's method with adjusted time-steps). The length will be determined by the number of iterations of the loop.

我想知道在我运行循环时是否有办法做到这一点,或者我是否需要在开始时进行设置,以及如何去做.

I'm wondering if there's a way I can do this while I'm running the loop or whether I need to set it up to begin with, and how to go about doing that.

推荐答案

如果列数是固定的,您可以随时向矩阵添加行(在循环内)

if the number of columns is fixed you can always add rows to your matrix (inside the loop)

例如

while (....)
   .....
   new_row =[x y] ; % new row with values x & y
   mat = [mat ; new_row]; 

当然,如果您知道 while 循环之前的迭代次数,则预先分配矩阵会更有效

of course if you know the number of iterations before the while loop it's more efficient to pre-allocate the matrix

这篇关于MATLAB中未知长度的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆