在 Matlab 中访问矩阵

发布于 2024-10-16 11:45:28 字数 1011 浏览 1 评论 0原文

假设用户输入数据如下。我定义我的矩阵是成本。我创建的矩阵是 3 x 3 矩阵。所以矩阵应该这样形成:

cost = [c11 c12 c13            

         c21 c22 c23

         c31 c32 c33]

因为我想显示行集,所以我这样做:

c1 = cost(1,:); % it will become c1 = c11 c12 c13

c2 = cost(2,:); % it will become c2 = c21 c22 c23

c3 = cost(3,:); % it will become c3 = c31 c32 c33

然后我想要矩阵中的值。我就是这样做的。

c11 = cost(1,1);

c12 = cost(1,2);

c13 = cost(1,3);

c21 = cost(2,1);

c22 = cost(2,2);

c23 = cost(2,3);

c31 = cost(3,1);

c32 = cost(3,2);

c33 = cost(3,3);

这就是我想用于此类矩阵的方程。

lambda = 
((8*c13*c23*c33*Pdt)+(4*c12*c23*c33)+(4*c13*c22*c33)+(4*c13*c23*c32)) ./ (4*c23*c33)+(4*c13*c33)+(4*c13*c23));

所以我的问题是,如果我想制作 4 x 3 矩阵,它会生成一个像这样的矩阵:

cost = [c11 c12 c13            

         c21 c22 c23

         c31 c32 c33

         c41 c42 c43]

我想用于这个矩阵(4 x 3)的方程是完全不同的。那我要怎么做呢?我需要使用 if else 语句吗?或者做一会儿?谁能帮我解决这个问题吗?任何人都可以创建代码吗?

Assume user input data as below. I define my matrix is cost. The matrix i created is 3 by 3 matrix. So the matrix should form like this:

cost = [c11 c12 c13            

         c21 c22 c23

         c31 c32 c33]

Since i want to display set of row, i do it like this :

c1 = cost(1,:); % it will become c1 = c11 c12 c13

c2 = cost(2,:); % it will become c2 = c21 c22 c23

c3 = cost(3,:); % it will become c3 = c31 c32 c33

Then i want the value in the matrix. I do it like this.

c11 = cost(1,1);

c12 = cost(1,2);

c13 = cost(1,3);

c21 = cost(2,1);

c22 = cost(2,2);

c23 = cost(2,3);

c31 = cost(3,1);

c32 = cost(3,2);

c33 = cost(3,3);

So this is the equation that i want to use for this type of matrix.

lambda = 
((8*c13*c23*c33*Pdt)+(4*c12*c23*c33)+(4*c13*c22*c33)+(4*c13*c23*c32)) ./ (4*c23*c33)+(4*c13*c33)+(4*c13*c23));

So my problem is, if i want to make 4 by 3 matrix, and it would generate a matrix like this:

cost = [c11 c12 c13            

         c21 c22 c23

         c31 c32 c33

         c41 c42 c43]

The equation that i want to use for this matrix(4 by 3) is quite different. So how im gonna do it? Do i need to use if else statement? or do while? Can anyone help me solve this? Can anyone create the code?

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

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

发布评论

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

评论(1

旧人九事 2024-10-23 11:45:28

为什么要显式创建变量 c11、c12、...?当然,像这样访问方程中的矩阵会更容易:

拉姆达 =
((8*成本(1,3)*成本(2,3)*成本(3,3)*Pdt)+(4*成本(1,2)*成本(2,3)*成本(3,3 )+(4*成本(1,3)*成本(2,2)*c(3,3))+(4*成本(1,3)*成本(2,3)*成本(3,2) ) ./
(4*成本(2,3)*成本(3,3))+(4*成本(1,3)*成本(3,3))+(4*成本(1,3)*成本(2, 3)));

对于你的问题,是的,只需使用一个简单的 if 语句,如下所示:

如果大小(成本,1) == 3

 %矩阵大小 3x3 的方程

其他

 %矩阵大小 4x3 的方程

Why do you explicitly create the variables c11, c12, ...? Surely it would be easier to just access the matrix in your equation like this:

lambda =
((8*cost(1,3)*cost(2,3)*cost(3,3)*Pdt)+(4*cost(1,2)*cost(2,3)*cost(3,3)+(4*cost(1,3)*cost(2,2)*c(3,3))+(4*cost(1,3)*cost(2,3)*cost(3,2)) ./
(4*cost(2,3)*cost(3,3))+(4*cost(1,3)*cost(3,3))+(4*cost(1,3)*cost(2,3)));

For your question, yes, just use a simple if statment, like this:

if size(cost,1) == 3

  %equation for matrix size 3x3

else

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