How can i do fast a sum of products?

2 次查看(过去 30 天)
Le Dung
Le Dung 2018-1-15
编辑: Matt J 2018-1-18
Hi everyone! My problem is: I have an array of matrix 2x2, [A],[B],....[Z] (elements in the matrix are integer (or complex) numbers) and an array: 1/(s-a),1/(s-b),....,1/(s-z). And a,b,c,...,z are also integer (or complex) numbers. But s is a variable, s=1 to 100. And, i must calculate:
H=[A]*1/(s-a)+[B]*1/(s-b)+.....+[Z]*1/(s-z)
So, If i want to create a code that do fast (exactly is general), how can i do? I created two variables consist of cells, H1=cell(n,1) where n is number of matrix. So, H1 is a row vector, nx1, H1 consist of the matrix above. H2=cell(1,n) where n is number of matrix. So, H2 is a column vector, 1xn, H2 consist of 1/(s-a),1/(s-b),....,1/(s-z).
So, instead of writing a code:
H=[A]*1/(s-a)+[B]*1/(s-b)+.....+[Z]*1/(s-z) % (of course, i need do: syms s)
i will write:
H=H1xH2, % (H is not only a matrix 2x2 but also function of s variable)
That is problem i want to talk. But, you know, Matlab returns a warning
Undefined function 'mtimes' for input arguments of type 'cell'.
So, who can help me? thank you so much.
  2 个评论
Matt J
Matt J 2018-1-15
编辑:Matt J 2018-1-15
H1 is a row vector, nx1...H2 is a column vector, 1xn
Things that are nx1 are columns and things that are 1xn are rows.
of course, i need do: syms s
Are you sure you need to do this symbolically? If speed is a priority, this is working against you.
Le Dung
Le Dung 2018-1-16
编辑:Le Dung 2018-1-16
First, thank you for your comment. Oh yes, I had a mistake. Of course, nx1 is a row vector, and 1xn is a column vector. Yes, i want to write a code that more general (not is running speed of code but my code is more general or exactly is more profession) as i described above. And, " i need do: syms s". That is only my ideal to solve this my problem. May be you can have different methods (code) to solve my problem. Could you help me? Again,thank you so much.

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2018-1-16
If you're not doing it symbolically, it becomes very simple matrix arithematic:
H1stack=cat(3,H1{:});
H2stack=cat(3,H2{:});
s=5; %for example
result=H1stack./(s-H2stack);
  4 个评论
Le Dung
Le Dung 2018-1-18
编辑:Le Dung 2018-1-18
Yes, but can "s" be a vector? for example, s=1:100. If not, if s=1:1000, you must call the function 1000 times according to each value of s. It is a inconvenience in the cases where s has many values unless you use a loop.
A=[1 2;3 4];
B=[5 6;7 8];
a=0.5;
b=3.5;
H1=cell(2,1);
H2=cell(1,2);
H1{1,1}=A;
H1{2,1}=B;
H2{1,1}=a;
H2{1,2}=b;
H1stack=cat(3,H1{:});
H2stack=cat(3,H2{:});
s=5;
result1=H1stack./(s-H2stack);
Matlab returns:
Array dimensions must match for binary array op.
Matt J
Matt J 2018-1-18
编辑:Matt J 2018-1-18
Array dimensions must match for binary array op.
To get rid of this, upgrade to R2016b or higher! If you can't, you'll have to use bsxfun().
Yes, but can "s" be a vector?
Yes, you can modify the code to
function result = myFunc(s,H1stack,H2stack)
s=reshape(s,1,1,1,[]);
result=H1stack./(s-H2stack);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by