1

I have a matrix in cell format, each element is a cell which contains two numbers, for example if c1 be the cell with index 1, i have c1 = (k1,k2). I want to do some operation for each cell for example something like this:

(k1-a) + (k2-b)

the main operation is:

[(k1-a1)/(z1-a1) * (k1-a2)/(z1-a2) * ....] * [(k2-b1)/(z2-b1) * (k2-b2)/(z2-k2) * ...] 

but i don't want to use for loop and i think do it for the whole matrix once is a faster way.
How is this possible?

13
  • 3
    Does your matrix need to be a cell array? If the contents are uniform, it can just be an N-dimensional matrix instead. This will make it easy to do what you're asking (efficiently).
    – tmpearce
    Apr 15, 2014 at 18:35
  • No, because of the operation for each cell i will receive a number. Apr 15, 2014 at 18:39
  • 1
    Is it an option to do cell2mat on your cell array?
    – Floris
    Apr 15, 2014 at 18:47
  • just two numbers, they are all numbers Apr 15, 2014 at 19:01
  • Sorry must have asked about a1, a2 .. b1, b2.. too. Where do we get those from?
    – Divakar
    Apr 15, 2014 at 19:03

1 Answer 1

2

One approach -

num2cell(reshape(sum(bsxfun(@minus,vertcat(C{:}),[a b]),2),size(C))) %%// C is input cell array

Edit 1: From all the little pieces of information I could gather of your question and comments, I am hoping this is what you need -

%%// **** INPUTS (Sample values taken here for demo). 
%%// Replace these with your values
AB = [{[1 0]} {[2 7]} {[2 2]} ; {[6 2]} {[7 6]} {[5 4]}]; %%// a-b cell array
c1 = {[2 3]}; %%// c1 cell array
z12 = {[4 3]}; %%// z12 cell array

%%// ***** Processing starts here
%%// Read in data into double matrices
c1mat = cell2mat(c1)
k1 = c1mat(1);
k2 = c1mat(2);

z12mat = cell2mat(z12)
z1 = z12mat(1);
z2 = z12mat(2);

ABmat = cell2mat(AB);
Amat = ABmat(:,1:2:end)
Bmat = ABmat(:,2:2:end)
Amat = Amat(:);
Bmat = Bmat(:);

%%// This is your [(k1-a1)/(z1-a1) * (k1-a2)/(z1-a2) * ....]
v1 = prod((k1-Amat)./(z1-Amat))

%%// This is your [(k2-b1)/(z2-b1) * (k2-b2)/(z2-k2) * ...]
v2 = prod((k2-Bmat)./(z2-Bmat))

%%// Final output
out = v1.*v2
5
  • It was an example dear Divakar for the operation, It is a little more complex. i will edit the question for more information Apr 15, 2014 at 18:56
  • @user3482383 Check out Edit 1!
    – Divakar
    Apr 15, 2014 at 19:54
  • How about if c1 be a cell array for example m by n? there is also another problem, for zome z1 and z2, they are equal to Amat or Bmat so the denominator will be zeros, among Amats or Bmats i want those which are not equal to z1 and z2 (Amat~=z1 and Bmat~=z2) Apr 15, 2014 at 20:27
  • Regarding c1, if it is of mxn size, so is it of the same size as AB cell array?
    – Divakar
    Apr 15, 2014 at 20:34
  • Just to clear out more confusions, could you post cell array sizes of AB (cell array holding a's and b's) and z12 (cell array holding z1 and z2), which should be 2x1 if it's just z1 and z2?
    – Divakar
    Apr 15, 2014 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.