Substitute syms in 2D array to have 3D array

1 回表示 (過去 30 日間)
Saurav Agarwal
Saurav Agarwal 2012 年 12 月 22 日
I have a 2D array of syms and I want to substitute the syms variables with 1D array. How do i do it? If a form a loop it takes a long runtime.
sample code:
syms a b c
arr=[0 0 0;0 0 0;a b c];
a=[1 2 3 4 5]';
b=[2 3 4 5 6]';
c=[5 6 7 8 9]';
subs(arr)
The output should be a 3x3x5 matrix. Please help
I have the following code using loops:
syms a b c
arr=[0 0 0;0 0 0;a b c];
J=zeros(3,3,5);
a1=[1 2 3 4 5]';
b1=[2 3 4 5 6]';
c1=[5 6 7 8 9]';
for i=1:5
a=a1(i);
b=b1(i);
c=c1(i);
J(i)=subs(arr);
end
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 22 日
It's not clear

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2012 年 12 月 22 日
subs() is not able to do this for you, as you need to replicate the other elements of the array. I do not think you will be able to do this without a loop (though you can probably hide the loop using arrayfun())
  1 件のコメント
Walter Roberson
Walter Roberson 2012 年 12 月 23 日
You initialize J as numeric zeros of size (3,3,5), but you assign to J(i) which would be J(i,1,1) a single numeric location.
Try
syms a b c
arr=[0 0 0;0 0 0;a b c];
a1=[1 2 3 4 5]';
b1=[2 3 4 5 6]';
c1=[5 6 7 8 9]';
J = double( arrayfun(@(aval, bval, cval) subs(arr, {a, b, c}, {aval, bval, cval}), a1, b1, c1) );
You might have to use
J = double( cell2mat( arrayfun(@(aval, bval, cval) subs(arr, {a, b, c}, {aval, bval, cval}), a1, b1, c1, 'Uniform', 0) ));

サインインしてコメントする。

その他の回答 (1 件)

Saurav Agarwal
Saurav Agarwal 2012 年 12 月 23 日
Taking a loop take a lot of time.. is there an alternate way? please help.
  2 件のコメント
Walter Roberson
Walter Roberson 2012 年 12 月 23 日
I cannot think of any way of doing this that does not involve a loop.
If you show the loop that you have coded, we can examine it and see if we can suggest improvements.
Saurav Agarwal
Saurav Agarwal 2012 年 12 月 23 日
I have edited the question to show the code using loop.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by