开发者

Accessing a matrix in Matlab

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.

lamb开发者_开发问答da = 
((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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜