필터 지우기
필터 지우기

How to subtract subtract two elements in the same matrix and then do that again and then divide these values by each other?

조회 수: 3 (최근 30일)
I want to make a for loop where I subtract two elements in the same matrix and then do that process again for another matrix, and then divide these two values together. Here is the code I have as an example of doing it once where the variable is pgf but I want to do it for the whole matrices using a for loop or something else:
x1 = [1e6:20000:5e6];
D_c = 5000;% in m
D1 = [0:25:5000];
x_o = 1e6; % in m
x_2 = [x_o:20000:5e6];
L = 4e6; % in m
Tw_o = 300; % in K
LR_w = 0.004; % in K/m
LR_c = -0.007; % in K/m
pz_0 = 1000; % in mb
D_1 = D_c.*(sin((pi/L).*(x1-x_o)));
T_c2 = Tw_o-(((LR_w-LR_c).*D_1)-((LR_c.*D1)));
g = 9.81;
Rd = 287;
p1 = pz_0*(((((T_c2)-(LR_c.*D1))./(T_c2))).^((g)/(Rd*LR_c)));
dens = 1.293;
f = (2)*(7.292e-5)*sind(45);
f2 = (1)/(dens*(2)*(7.292e-5)*sind(45));
pgf = (p1(1:1)-p1(1:2))/((x1(1:2)-x1(1:1))); % example of what I am trying to do for all elements
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 10월 10일
Are you sure??
p1(1:2) is a vector, and x1(1:2) is a vector, so you have two vectors with / as the operation between them. The / operator is mrdivide, / which is approximately A/B ==> A * pinv(B) where * is the matrix multiplication operator.
The / operator is not element-by-element division. which is the ./ operator
the cyclist
the cyclist 2023년 10월 10일
@Matthew, did you perhaps mean
p1(1,1)-p1(1,2)
etc, which would be the difference between two matrix elements, as you describe?
Still, it is not perfectly clear to me what you intend. Maybe you could tell us what the result would be for two small matrices, like
p1 = [2 3 5;
7 11 13];
x1 = [17 23 29;
31 37 41];
I expect you will not even need a loop, because MATLAB is designed to do many operations on whole matrices.

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

답변 (1개)

Ayush Modi
Ayush Modi 2023년 10월 10일
Hi Matthew,
I understand you would like to subtract the adjacent values in the "p1" and "x1" matrices. And then, you would like to do element wise division for the two resultant matrices.
Here is an example showing how you can implement the same using "diff" function:
resSub1 = diff(p1, 1, 2); % This does secondElement - firstElement, thirdElement - secondElement and so on..
resSub2 = diff(x1, 1, 2);
result = resSub1./resSub2; % Element wise division of resSub1 and resSub2
You can refer to the following MathWorks documentation for more information about "diff" function:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by