필터 지우기
필터 지우기

Problem with matrix manipulation

조회 수: 1 (최근 30일)
slumberk
slumberk 2011년 2월 9일
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. How can I do that? Do I need to use if else statement? Or do while? Can anyone help me solve this? Can anyone create the code?

채택된 답변

Rob Graessle
Rob Graessle 2011년 2월 10일
You can use the SIZE function to check how many rows are in the cost matrix, then use IF/ELSE to change the equation used for lambda.
if size(cost, 1) == 3
% Use lambda equation for 3 rows
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)*cost(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));
elseif size(cost, 1) == 4
% Use lambda equation for 4 rows
lambda = ...;
end
Also note that you don't need to create new variables c11, c12, etc. - it's not very memory-efficient.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 2월 10일
It is not recommended that you use this approach at all. Please see the FAQ for alternatives.

slumberk
slumberk 2011년 2월 10일
@rob
what is the meaning of size(cost,1) == 3?? Does it have 3 row n column??
@walter
thx for the info =)
  댓글 수: 2
Rob Graessle
Rob Graessle 2011년 2월 10일
The '1' argument means you want the size along the first dimension (rows). So this line checks that cost has 3 rows. If you wanted to check the number of columns you would use size(cost,2). Or to get both:
[r,c] = size(cost)
where r is the number of rows and c is columns.
slumberk
slumberk 2011년 2월 11일
@rob
thx rob =)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by