Sum two cells and write to a third cell

I have three cells A{10}, B{10} and C{10} of size 5x5. On each cell I want to do: A(:,1) = B(:,1) + C(:,1) I might also want to do: A(2:4,5) = B(2:4,5) + C(2:4,5)
Currently if I just write something like above, I will get error as Index exceeds matrix dimensions.
Any ideas on how this can be done (as fast as possible)

 Respuesta aceptada

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon el 11 de Mayo de 2015
Editada: Alfonso Nieto-Castanon el 11 de Mayo de 2015
Probaby a loop is going to be fastest, e.g.:
for n=1:numel(A)
A{n}(:,1)=B{n}(:,1)+C{n}(:,1);
end
but if you really dislike for loops you could always do something like:
A = cellfun(@(a,b,c)[b(:,1)+c(:,1) a(:,2:end)], A, B, C, 'uni',0);

3 comentarios

User05
User05 el 11 de Mayo de 2015
Editada: User05 el 11 de Mayo de 2015
The for loop doesnt seem to be working. I used it as below:
in1 = cell(1,5);
in1(:) = {int32(zeros(10,10))};
in2 = cell(1,5);
in2(:) = {int32(ones(10,10))};
for n=1:5
out{n}(:,1)=in1{n}(:,1)+in2{n}(:,1);
end
but the desired output is :
tin1 = in1{1};
tin2 = in2{1};
in1{1} = tin1(:,1) + tin2(:,1);
I only want the first col to get afected, rest all shold remain same
Alfonso Nieto-Castanon
Alfonso Nieto-Castanon el 11 de Mayo de 2015
Editada: Alfonso Nieto-Castanon el 11 de Mayo de 2015
not sure what you mean by:
in1{1} = tin1(:,1) + tin2(:,1);
that will change the variable in1 instead of the variable out, it will make the first element of that cell array a 10 by 1 vector instead of a 10 by 10 matrix, and it will only modify that first element of the in1 cell array and none of the other elements. Is that what you really want?
If, instead, what you want is to change all five elements of out, but only the first column of each, and keep the rest of those 10x10 matrices the same as they were, then use the original code again (but first initialize out to something that shows you unambiguously what happened, e.g.
out = repmat({randn(10)}, 1,5);
Sorry that in1 was typo. Initializing out to something in beginning works. Below code achieves what I want to do:
in1 = cell(1,5);
in1(:) = {int32(zeros(10,10))};
in2 = cell(1,5);
in2(:) = {int32(ones(10,10))};
out = cell(1,5);
out(:) = {int32(zeros(10,10))};
for n=1:5
out{n}(:,1)=in1{n}(:,1)+in2{n}(:,1);
end

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 11 de Mayo de 2015
Editada: Walter Roberson el 12 de Mayo de 2015

0 votos

cellfun()
However, the method to do it "as fast as possible" is likely to involve creating some mex code written in C and optimizing the heck out of it. It would probably take you a few months to learn how to do properly -- I know the resident expert James Tursa is still learning about using mex after what must be at least 8 years of working with it.

3 comentarios

User05
User05 el 11 de Mayo de 2015
Editada: User05 el 11 de Mayo de 2015
From what I have seen I can use cellfun() when I am applying the fucntion on the entire cell. In my case I only want to apply to certain rows and cols. Any clues on how this can be done using cellfun()
Thanks
Update: If I use code:
in1 = cell(1,5);
in1(:) = {int32(zeros(10,10))};
in2 = cell(1,5);
in2(:) = {int32(ones(10,10))};
out(1,:) = cellfun(@plus, in1(1,:), in2(1,:) ,'UniformOutput', false);
out has sum of in1 and in2, what I want is only the first col of out in each of 5 cell, should have value 1. Can I achieve that using cellfun
Use an anonymous function.
A = cellfun(@(a, b, c) [a(:,1:end-1), b(:,1) + c(:,1)], A, B, C, 'Uniform', false);
I tried this anonymous function suggestion, but I dont quite get whats it exactly doing. My code:
in1 = cell(1,5);
in1(:) = {int32(zeros(10,10))};
in2 = cell(1,5);
in2(:) = {int32(ones(10,10))};
out = cell(1,5);
out(:) = {int32(zeros(10,10))};
out = cellfun(@(a, b, c) [a(:,1:end-1), b(:,1) + c(:,1)], out, in1, in2, 'Uniform', false);
After execution I see that 10th col of each cell in out is getting assigned value 1. What I wanted and expected was the 1st col should be all 1. So isnt that what is suppose to happen when I write b(:,1)+c(:,1)? Also what is a(:,1:end-1) doing?

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 11 de Mayo de 2015

Comentada:

el 12 de Mayo de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by