How to improve the code for vector calculation

조회 수: 1 (최근 30일)
krishnamurthy
krishnamurthy 2013년 6월 13일
clear all
clc
NG=3;
B_area=rand(12,3);
P=rand(1,12)
P1=P(1:3);P2=P(4:6);P3=P(7:9);P4=P(10:12);
B_area1=B_area((1:3),:);
B_area2=B_area((4:6),:);
B_area3=B_area((7:9),:);
B_area4=B_area((10:12),:);
for i=1:NG
for j=1:NG
PL_area1(j)=P1(i).*B_area1(i,j).*P1(j);
PL_area2(j)=P2(i).*B_area2(i,j).*P2(j);
PL_area3(j)=P3(i).*B_area3(i,j).*P3(j);
PL_area4(j)=P4(i).*B_area4(i,j).*P4(j);
end
eval(['PL_area1' num2str(i) '=(PL_area1)']);
eval(['PL_area2' num2str(i) '=(PL_area2)']);
eval(['PL_area3' num2str(i) '=(PL_area3)']);
eval(['PL_area4' num2str(i) '=(PL_area4)']);
end
PL_area1=sum(PL_area11(:,:)+PL_area12(:,:)+PL_area13(:,:));
PL_area2=sum(PL_area21(:,:)+PL_area22(:,:)+PL_area23(:,:));
PL_area3=sum(PL_area31(:,:)+PL_area32(:,:)+PL_area33(:,:));
PL_area4=sum(PL_area41(:,:)+PL_area42(:,:)+PL_area43(:,:));
PL=[PL_area1 PL_area2 PL_area3 PL_area4]

답변 (2개)

Andrei Bobrov
Andrei Bobrov 2013년 6월 13일
B = rand(12,3);
P = rand(1,12);
Ba = permute(reshape(B,3,4,[]),[1 3 2]);
Pn = reshape(P,3,1,[]);
Pa = bsxfun(@times,Pn,reshape(Pn,1,3,[])).*Ba;
PL = sum(reshape(Pa,[],4));
  댓글 수: 1
krishnamurthy
krishnamurthy 2013년 6월 13일
Thanks a lot
Its really improved the code better

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


Iain
Iain 2013년 6월 13일
Inner looop can be replaced by:
PL_area4=P4(i).*B_area4(i,:).*P4(:); etc.
The outer loop cannot be replaced unless you can formulate your mathematics into a matrix form.
  댓글 수: 1
krishnamurthy
krishnamurthy 2013년 6월 13일
Sorry,
Can you explain the code you post..
I wont understood fully ..Even that code given error

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by