フィルターのクリア

Reduction of a matrix

1 回表示 (過去 30 日間)
Homayoon
Homayoon 2016 年 8 月 17 日
回答済み: Homayoon 2016 年 8 月 17 日
Dear MATLAB Experts,
I have encountered a problem in my research seems to be very tricky to me. Assume there is a matrix of size n*2 and the reduced form is needed. For simplicity let M represents our matrix. For each row of i, I need to calculate the difference of
M(i+1,1) - M(i,2);
and if the difference was smaller than a threshold value of 100, the the row numbers i and i+1 will be combined as a single row with the following elements:
M(i,1) M(i+1,2).
Otherwise, both rows i and i+1 should be kept. The output of such a code is my desired reduced matrix. For example consider matrix M as
M = [5 180
200 300
330 600
1000 1400
1430 1500]
The code firstly combine the first two rows since they meet the criterion explained above;
M1 = [5 300
330 600
1000 1400
1430 1500]
In the second step, the code will combine the first two rows of the output matrix of step 1, i.e. M1 as follows
M2= [5 600
1000 1400
1430 1500]
This procedure continues so far so forth until we get the final desired output that is,
M_RED = [5 600
1000 1500]
I wonder if you can kindly help me figure it out. Any helps will be appreciated and I thank you for the time you take to assist me.
PS: What I actually need to get is the M_RED and I really do not care what the M1 and M2 and other matrices look alike. I just introduced them for the sake of better illustration.

採用された回答

Homayoon
Homayoon 2016 年 8 月 17 日
This is what I came up with but I am looking for a more sophisticated approach.
b=0
for i=1:(size(M,1)-1)
if M(i+1,1)-M(i,2)>101
b=b+1;
end
end
z = zeros(b+1 ,2); %reduced matrix
c=1;
z(1)=M(1);
z(2*(b+1))=M(size(M,1)*2);
for i=1:4
if M(i+1,1)-M(i,2)>101
z(c,2) = M(i,2);
z(c+1,1)=M(i+1,1);
c=c+1;
end
end
This works perfectly but as I said am looking for something more sophisticated.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by