How do I solve an equation using two 1D matrices of different sizes and ranges as inputs?

조회 수: 2 (최근 30일)
This is seemingly basic, but for whatever reason I am unable to find the solution on my own or on the forums. I am attempting to solve for x, where y and z are 1D matrices, using any combination of 2 numbers, 1 number from each. For the sake of simplicity I think the same solution could be found assuming x=y*z+z, where y=1:3 and z=1:3. Note the addition of 'z' after multiplication does not allow for simply multiplying matrices, but requires selecting the individual values and applying them to the given equation for all possible combinations. I know there is a combination function, which my be required for this solution, but I am unaware of how to apply it here.

채택된 답변

EssJaySeeJr.
EssJaySeeJr. 2016년 3월 23일
This.
T1= evalin('base','T1');
T3= evalin('base','T3');
[x,sT1]=size(T1);
[y,sT3]=size(T3);
r1=1;
r2=1;
r3=1;
cT1=1;
cT2=1;
cT3=1;
T13=0;
while cT1<sT1+1
T13a=0;
iter=1;
for iter=1:sT3
T13a(iter)=T1(1,cT1);
T13(r1,1)=T13a(iter)';
T13(r1,2)=T3(r3,cT3);
r1=(r1)+1;
cT3=(cT3)+1;
end
cT3=1;
cT1=(cT1)+1;
end
[sT2,z]=size(T13);
T2=0;
while cT2<sT2+1
T2(cT2,r2)=(((L1*T13(cT2,1))/L2)+T13(cT2,2))/(1+(L1/L2));
cT2=(cT2)+1;
end
I know this can use cleaning up, but obviously I am not very proficient with MATLAB. Either way, this does what I need it to do, how I need it to do it, regardless of the two input matrices sizes or ranges. Now if this should be or can be a function, that's another story. Right now I am content with just running the script as is. I will also probably find a way to combine T13 and T2 and plot in 3D. Not sure how to do this at the moment, but I already got this far...

추가 답변 (2개)

Star Strider
Star Strider 2016년 3월 21일
I’m not sure what you want.
One of these will likely work:
y=1:3;
z=1:3;
x1 = y * z.';
x2 = y.' * z;
x3 = bsxfun(@times, y, z);
  댓글 수: 10
Star Strider
Star Strider 2016년 3월 23일
What you want to do is what the meshgrid approach does. It performs the calculation on all combinations of all the elements in your vectors in the resulting matrix. You simply have to define the results you want in your vector. The diagonal of the resulting matrix will reproduce the vector your function creates.
Take a look at the resulting matrix of my most recent Comment before this one. You likely won’t need a loop, just an addressing scheme to get the elements you want.
EssJaySeeJr.
EssJaySeeJr. 2016년 3월 23일
I answered my own question :D If you look, you will understand exactly what I was looking for. I think some things were getting lost in my lack of translation. Thanks, your input did ultimately help.

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


Torsten
Torsten 2016년 3월 22일
y=[1 2 3];
z=[1 2 3];
x=y'*z;
Best wishes
Torsten.
  댓글 수: 3
Torsten
Torsten 2016년 3월 22일
Use
x=reshape(x',1,9)
at the end, and you will get the x-vector you want.
Best wishes
Torsten.
EssJaySeeJr.
EssJaySeeJr. 2016년 3월 22일
This solution can't be applied to anything other than matrix arithmetic. Please note the update to the question. I apologize for not properly conveying my problem initially. Thank you for your time.

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

카테고리

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