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

조회 수: 3 (최근 30일)
CodeJunkie
CodeJunkie 2019년 1월 29일
댓글: CodeJunkie 2019년 1월 29일
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