필터 지우기
필터 지우기

Avoid for loops, use cell operation to do: r2' * r1 * l1' * l2

조회 수: 2 (최근 30일)
Xh Du
Xh Du 2017년 5월 9일
댓글: Stephen23 2017년 5월 9일
Hi all,
Say I have 2 simple 1-by-2 cells, I'd like to perform operation like this: r2' * r1 * l1' * l2 to obtain a 2-by-2 matrix, sample code as follows:
clear; clc;
% the cells.
bl = {1 2};
br = {3 4};
% element 1.
l1 = bl{1, 1};
l2 = bl{1, 1};
r1 = br{1, 1};
r2 = br{1, 1};
otpt11 = r2' * r1 * l1' * l2;
% element 2.
l1 = bl{1, 2};
l2 = bl{1, 1};
r1 = br{1, 2};
r2 = br{1, 1};
otpt12 = r2' * r1 * l1' * l2;
% element 3.
l1 = bl{1, 1};
l2 = bl{1, 2};
r1 = br{1, 1};
r2 = br{1, 2};
otpt21 = r2' * r1 * l1' * l2;
% element 4.
l1 = bl{1, 2};
l2 = bl{1, 2};
r1 = br{1, 2};
r2 = br{1, 2};
otpt22 = r2' * r1 * l1' * l2;
% put all elements together.
otpt = [otpt11 otpt12; otpt21 otpt22];
I can probably use for loops to do it, but I'd like to use a compact operation such as cellfun to do it, which I believe is faster and more nature for cells.
Many thanks!
  댓글 수: 6
Jan
Jan 2017년 5월 9일
Use loops. They are fast and clean, if you pre-allocate the output.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by