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

3 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by