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

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

问题描述

我正在尝试建立一个具有两列的可变长度的零矩阵,我可以在其中输出while循环的结果(目的是使用它来存储经过调整的时间步长的Euler方法的步长数据).该长度取决于循环的迭代次数.

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天全站免登陆