matlab中的字符编程

发布于 2024-12-14 00:32:14 字数 276 浏览 1 评论 0原文

我有包含字符 time1、time2、time3 直到 time60 的数据。这意味着每次都有自己的结果,例如time1=70time2=56等等...如何将这些数据排列在矩阵中而不需要手动排列他们是:

time=[time1 time2 time3 time4 time5.......time60].

除了上述步骤之外,我不知道。上述步骤需要花费更多时间才能输入 60 个数据。有没有最简单的方法来排列这些数据?

I have data with character time1, time2,time3 until time60. Which means each time have their own result,for example time1=70,time2=56 and etc.... how to arrange this data in matrix without need to manually arrange them as:

time=[time1 time2 time3 time4 time5.......time60].

I have no idea aside from above step. The above step is taking more time in order to type until 60 data. Is there any easiest way to arrange these data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-12-21 00:32:14

您可以在 for 循环内使用 eval 命令。

time = [];
for n = 1:60
    eval(['time = [time, time', num2str(n), '];']);
end

You can use the eval command inside a for-loop.

time = [];
for n = 1:60
    eval(['time = [time, time', num2str(n), '];']);
end
柠檬色的秋千 2024-12-21 00:32:14

您可以直接执行此操作,无需循环,如下所示。

eval(['time=[' sprintf('time%d ',1:60) '];'])

但是,将来,请尝试不要将变量命名为 time1time2 等。相反,请考虑将值存储在向量中如时间(1)=...时间(2)=...。这使您的代码更清晰,更少的变量使您的工作区变得混乱,可以利用 MATLAB 真正擅长的矢量化运算(这取决于您使用它做什么......)以及如果您需要将值保存到 MAT 文件,您只需要保存一个变量而不是 60 个。

我还建议尽可能不要使用 eval,并且仅在无法避免的情况下使用(例如,与其他人一起工作代码/数据)。

You can do it straightforwardly without a loop as

eval(['time=[' sprintf('time%d ',1:60) '];'])

However, in future, try not to name your variables as time1, time2, etc. Instead, consider storing the values in a vector as time(1)=..., time(2)=.... This makes your code cleaner, fewer variables cluttering your workspace, can take advantage of vectorized operations which MATLAB is really good at (this depends on what you do with it though...) and if you need to save the values to a MAT file, you only need to save a single variable instead of 60.

I would also recommend against using eval as much as possible, and only in cases where it can't be avoided (e.g., working with someone else's code/data).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文