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

3 次查看(过去 30 天)
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

采纳的回答

Guillaume
Guillaume 2019-1-29
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

更多回答(1 个)

Luna
Luna 2019-1-29
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})')';

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by