Clear Filters
Clear Filters

Reduction of a matrix

1 view (last 30 days)
Homayoon
Homayoon on 17 Aug 2016
Answered: Homayoon on 17 Aug 2016
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.

Accepted Answer

Homayoon
Homayoon on 17 Aug 2016
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!