在 Matlab 中访问矩阵
假设用户输入数据如下。我定义我的矩阵是成本。我创建的矩阵是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要显式创建变量 c11、c12、...?当然,像这样访问方程中的矩阵会更容易:
对于你的问题,是的,只需使用一个简单的 if 语句,如下所示:
Why do you explicitly create the variables c11, c12, ...? Surely it would be easier to just access the matrix in your equation like this:
For your question, yes, just use a simple if statment, like this: