Multiplying a matrix in a cell by another matrix in the same cell

3 vues (au cours des 30 derniers jours)
CodeJunkie
CodeJunkie le 29 Jan 2019
Commenté : CodeJunkie le 29 Jan 2019
RU and Difference are 5x1 cells, each cell has a (25,1) double inside of it. I would like to multiply cell RU{1,1} by RU{2,1} by RU{3,1 }until RU{n,1} and the same for difference. By my estimation that would give me a (25,1) double or a 1x1 cell depending on how you do it. Ive tried looping it as well as cellfun and mat2cell and cell2mat but have not been able to solve this issue. Any help anyone can lend would be much appreciated.
%% **
PRU=cell(1,1)';
PD=cell(1,1)';
for iter=1:n
PRU{1,1} =RU{iter,1}*RU{iter+1,1}
PD{1,1} =Difference{iter,1}*Difference{iter+1,1}
end

Réponse acceptée

Guillaume
Guillaume le 29 Jan 2019
Since each cell array contains matrices that are all the same size (in this case, column vectors), you would make your life much easier by using matrices instead of cell arrays. Your multiplication operation would then be trivial
RUasmatrix = [RU{:}]; %convert 5x1 cell array of 25x1 column vectors into a 25x5 matrix
PRU = prod(RUasmatrix, 2) %multiply all the columns. No point in storing that in a cell array
  2 commentaires
Luna
Luna le 29 Jan 2019
@Guillaume +1 :)
CodeJunkie
CodeJunkie le 29 Jan 2019
Thanks again I appeciate the help.

Connectez-vous pour commenter.

Plus de réponses (1)

Luna
Luna le 29 Jan 2019
Try this:
RU = {randi(20,25,1),randi(20,25,1),randi(20,25,1),randi(20,25,1),randi(20,25,1)}';
result = prod(horzcat(RU{1:numel(RU),1})')';

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Produits


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by