MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Matlab Help: Matrix Reduction Algorithm ???

Asked by Usman Ali on 7 May 2012

hello every one.. i am a bit new to matlab so have no vocabulary of commands or how thing work as vector matrices or etc etc.. so i would like to develop a prototype or an algorithm for scenario reduction in matlab( each row of a matrix is called as single scenario).

the steps of this algorithms are

1) for any given matrix, find the distance matrix which show euclidean distance between each and every row of original matrix.

2) find the min: value in distance matrix.

3) delete that row with min elemenet value and with min: probabilty of occurance.

4) construct seperate matrix which contain only these deleted rows

5) reconstruct distance matrix with preserved row (without deleted rows).

6) repeat untill the desired numb of rows are preserved.

So I started with a simple 3x3 matrix and i wrote few lines as follow.

--------------------------------------

% suppose any matrix e.g

x=[5 3 1; 2 5 6; 1 3 2];

dist_mat = squeeze(sqrt(sum(bsxfun(@minus,x,reshape(x',1,size(x,2),size(x,1))).^2,2)));

dist_mat(~dist_mat)=inf;

row_min = min(dist_mat);

min_value = min (row_min);

[r c]= find(d==min_value); % shows the position of the minimum element, r= which row and c= which column

so the results i get are

d =

```       Inf    6.1644    4.1231
6.1644       Inf    4.5826
4.1231    4.5826       Inf```

min_value =

`    4.1231`

r =

```     3
1```

c =

```     1
3```

thanks to 'Sean de Wolski' for correcting the dist_mat command.

in my e.g, the min value is in [3,1] and [1,3]. i.e D13=D31, this is correct.

let every row has its own probability, i.e P1= 0.2, P2=0.4, P3=0.4 (total P=1) since the min value = 4.1231 lying in 1st and 3rd row, we can delete either Row1 or Row3, but since the probabilty of Row1 < Row3 so row1 should be selected for deletion. i do not have any thing in mind how to delete this row, rearrange the dist_mat and write a new mat with this deleted row.

I m really sorry for so long post but i wanted to make thing more clear... i hope to get some +ve responses...

## Products

No products are associated with this question.

Answer by Johann on 8 Aug 2012
Edited by Johann on 16 Aug 2012

Hello Usman Ali,

the following code works for me:

```    % scennum_fav is the favored scenario number after reduction
scennum_fav=250;
% Initial scenario numbers, my matrix to be reduced is CO2_scen [1000x234]
scennum_int=size(CO2_scen,1);
% In the beginning all scenarios are equiprobable
scenprob=ones(scennum_int,1)*1/scennum_int;```
```% scenario reduciton algorithm
while size(CO2_scen,1)>scennum_fav
```
`    % Calculate euclidean distances`
```    dist_mat = queeze(sqrt(sum(bsxfun(@minus,CO2_scen,reshape(CO2_scen',1,size(CO2_scen,2),size(CO2_scen,1))).^2,2)));
dist_mat(~dist_mat)=inf;
row_min = min(dist_mat);
min_value = min (row_min);```
`        [row column]= find(dist_mat==min_value); % shows the position of the minimum element, r= which row and c= which column`
`    % Remove scenario with smallest distance and adding its probability to reference scenario (the scenario to be removed is independent of its probability)`
```        if scenprob(row(1))>scenprob(row(2))
CO2_scen(row(2),:)=[];
scenprob(row(1),:)=scenprob(row(1),:)+scenprob(row(2),:);
scenprob(row(2),:)=[];
elseif scenprob(row(1))<scenprob(row(2))
CO2_scen(row(1),:)=[];
scenprob(row(2,:))=scenprob(row(2,:))+scenprob(row(1,:));
scenprob(row(1),:)=[];
else
CO2_scen(row(2),:)=[];
scenprob(row(1,:))=scenprob(row(1,:))+scenprob(row(2,:));
scenprob(row(2),:)=[];
end
end```

I hope this was helpful even though your question was a few month ago. Johann

Dear Johann,

This is Kantrovich Method?

Johann on 19 Jul 2013

unfortunately I dont know Kantrovich.