Obtaining a sub-matrix based on some condition

조회 수: 3 (최근 30일)
Raghavendra Reddy P
Raghavendra Reddy P 2017년 7월 31일
댓글: Raghavendra Reddy P 2017년 7월 31일
Hello, I have a 10*3 matrix. 1st column represents Type of load(T), 2nd & 3rd are active(P) and Reactive (Q) Power loads. T columns contains 1, 2, 3 numbering indicating residential, commerial and industrial type of load.
Now, i just want a P and Q column such that if T=1, i wanted to multiply corresponding row of P & Q values with 3, similary if T=2 (and T=3), i wanted to multiply corresponding row of P & Q with 5 and 7 respectively. finally, i want a matrix 10*2, which contains only P&Q columns. i have attached document for better understanding. Please help me in this regards.

채택된 답변

KSSV
KSSV 2017년 7월 31일
L = [1 2 1 3 3 3 1 3 1 2 ;400 40 90 80 55 45 35 40 30 20 ; 60 30 0 15 55 0 30 30 15 20]';
iwant = zeros(size(L)) ;
idx = unique(L(:,1)) ;
Val = [3 5 7] ;
for i = 1:length(idx)
pos = L(:,1)==idx(i) ;
iwant(pos,:) = [L(pos,1) Val(i)*L(pos,2) Val(i)*L(pos,3)] ;
end
  댓글 수: 3
Raghavendra Reddy P
Raghavendra Reddy P 2017년 7월 31일
Attempted to access Val(4); index out of bounds because numel(Val)=3.
Error in loadvary (line 30) iwant(pos,:) = [L(pos,1) Val(i)*L(pos,2) Val(i)*L(pos,3)]
Raghavendra Reddy P
Raghavendra Reddy P 2017년 7월 31일
Sir, Thank you. I got it. your answer was right. there was some mistake with my matrix. :)

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

추가 답변 (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