29 views (last 30 days)

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.

Matt J
on 16 Jan 2018

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);

Matt J
on 17 Jan 2018

Yes, but you can re-execute the code I presented for any value of s that you wish. Put it inside a function if you like and call the function for different values of s,

function result = myFunc(s,H1stack,H2stack)

result=H1stack./(s-H2stack);

end

Matt J
on 18 Jan 2018

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

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/377226-how-can-i-do-fast-a-sum-of-products#comment_524802

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/377226-how-can-i-do-fast-a-sum-of-products#comment_524802

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/377226-how-can-i-do-fast-a-sum-of-products#comment_524982

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/377226-how-can-i-do-fast-a-sum-of-products#comment_524982

Sign in to comment.