MatLab:把我的asnwer变成矩阵格式21x1

时间:2015-05-06 17:57:21

标签: matlab

for t = 0:20
    y1 = 2*t*x + 2*b1 + b2
end

如何将答案放入矩阵形式。我应该有四个21x1矩阵。 我试过这个

for t = 0:20
    y1 = [2*t*x + 2*b1 + b2]
end

但它一直在给我

y1 =

    4.5000


y2 =

    5.1227


y3 =

   -0.3312


y4 =

   -4.4375


y1 =

    6.5000


y2 =

    7.1012


y3 =

    2.9776


y4 =

   -3.2167


y1 =

    8.5000


y2 =

    8.3758


y3 =

    4.2008


y4 =

   -0.5430


y1 =

   10.5000


y2 =

   10.6748


y3 =

    5.7916


y4 =

    2.3107


y1 =

   12.5000


y2 =

   12.7804


y3 =

    7.8259


y4 =

    3.6318


y1 =

   14.5000


y2 =

   15.3632


y3 =

   11.0662


y4 =

    5.8714


y1 =

   16.5000


y2 =

   16.4324


y3 =

   12.0206


y4 =

    7.6385
........

3 个答案:

答案 0 :(得分:0)

您可以在没有for循环的情况下执行此操作。对于y1案例,假设xb1b2是常量:

t = 0:20;
y1 = 2*t*x + 2*b1 + b2;

答案 1 :(得分:0)

./task/server.js

试试这个。您需要在每次循环迭代时将答案放在不同的位置。现在你覆盖它。

答案 2 :(得分:0)

如果您的目标是拥有四个" 21 x 1"矩阵,只需执行eigenchris所说的并在循环外执行此操作:

t = 0:20;
y1 = 2*t*x + 2*b1 + b2;
y2 = ...;
y3 = ...;
y4 = ...;

如果您想在循环内执行此操作,请在循环内预分配 y1, y2, y3, y4,然后索引的空间,或每次附加结果。您的for循环会在循环中的每次迭代中覆盖y1, y2, y3, y4并且不保存结果

方法#1

t = 0:20;
y1 = zeros(numel(t), 1);
y2 = y1; y3 = y1; y4 = y1;
for idx = 1 : numel(t)
    y1(idx) = 2*t(idx)*x + 2*b1 + b2;
    y2(idx) = ...;
    y3(idx) = ...;
    y4(idx) = ...;
end

确保在t之后为每个等式t(idx)编制y1索引。

方法#2

y1 = []; y2 = []; y3 = []; y4 = [];
for t = 0 : 20
    y1 = [y1; 2*t*x + 2*b1 + b2];
    y2 = [y2; ...];
    y3 = [y3; ...];
    y4 = [y4; ...];
end