2

I have this matrix:

mpc.bus = [100  1   170 100 0   0   1   1   0   230 1   1.1 0.9;
110 3   50  30  0   0   1   1   0   230 1   1.1 0.9;
120 2   80  45  0   0   1   1   0   230 1   1.1 0.9;
130 1   200 110 0   0   1   1   0   230 1   1.1 0.9;
140 1   30  8   0   0   1   1   0   230 1   1.1 0.9]

I want to create a sub-matrix that obeys the following rule: if the value of the second column is equal to 3 or 2, a new matrix (called MatrixPVREF) will receive the 1st, 2nd, 3rd and 4th column of mpc.bus.

This is the result I would like to obtain:

MatrixPVREF = [110  3   50  30;
120 2   80  45]
Adriaan
  • 17,741
  • 7
  • 42
  • 75

2 Answers2

2
mpc.bus = [100  1   170 100 0   0   1   1   0   230 1   1.1 0.9;
110 3   50  30  0   0   1   1   0   230 1   1.1 0.9;
120 2   80  45  0   0   1   1   0   230 1   1.1 0.9;
130 1   200 110 0   0   1   1   0   230 1   1.1 0.9;
140 1   30  8   0   0   1   1   0   230 1   1.1 0.9];

tmp2 = mpc.bus(mpc.bus(:,2)==2,:);
tmp3 = mpc.bus(mpc.bus(:,2)==3,:);

MatrixPVREF = [tmp3(1:4); tmp2(1:4)]


MatrixPVREF =

   110     3    50    30    
   120     2    80    45

Shorter:

MatrixPVREF = mpc.bus(mpc.bus(:,2)==2 | mpc.bus(:,2)==3,1:4);

Using logical indexing into mpc.bus.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
2

Use ismember to check if the elements in the second column equal to 2 or 3. Use matrix indexing to do the rest.

MatrixPVREF = mpc.bus(ismember(mpc.bus(:,2),[2 3]),1:4);

>> MatrixPVREF

MatrixPVREF =

   110     3    50    30    
   120     2    80    45
Sardar Usama
  • 19,536
  • 9
  • 36
  • 58