Matrix addition of a first column in a loop

1 vue (au cours des 30 derniers jours)
Veera Kanmani
Veera Kanmani le 29 Mar 2018
Modifié(e) : Guillaume le 29 Mar 2018
i have a cell A with 'n' cells inside. inside each cell i have a matrix. i want to do matrix addition of all the first column of these matrices inside a for loop. how to do it?
for i=1:n
% matrix addition code
end
  1 commentaire
KALYAN ACHARJYA
KALYAN ACHARJYA le 29 Mar 2018
Can you elaborate your question?

Connectez-vous pour commenter.

Réponse acceptée

Guillaume
Guillaume le 29 Mar 2018
Modifié(e) : Guillaume le 29 Mar 2018
With a loop:
colsum = yourcellarray{1}(:, 1);
for cidx = 2:numel(yourcellarray)
colsum = colsum + yourcellarray{cidx}(:, 1);
end
With cellfun:
colsum = sum(cell2mat(@(m) m(:, 1), yourcellarray, 'UniformOutput', false)), 2);
Either way, the matrices in each cell must have the same height.
Note that if all the matrices in the same array are the same size, then storing them as an N-d array (where N = ndim(yourmatrices)+1) would make your life much easier:
allmatrices = cat(ndim(yourcellarray{1})+1, yourcellarray{:});
colsum = sum(allmatrices(:, 1), ndim(allmatrices));

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by