Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Calculate differences through an array, by means of local absolute references inside of a matrix. And saving results.

Asked by Miguelcm90 on 9 Sep 2013

Dear Friends,

I would need to do the same as I asked in the question: "how to calculate differences inside an array , looping through a matrix and saving results" I do not want to repeat the question so as not to be repetitive.

I got this answer.

    [a,b]=unique(x(:,1),'stable');
    b=[b;size(x,1)];
    out=nan(size(x));
    for k=1:numel(b)-1
      i1=b(k)+1;
      i2=b(k+1)-1;
      out(i1:i2,:)=bsxfun(@minus,x(i1:i2,:),x(i1-1,:));
    end
    out(isnan(out(:,1)),:)=[];
    out=out(:,2:end)

Taking into account the results provided in this wire, below are shown only a sample of them see original file in:

https://www.dropbox.com/s/kpifg9z43x4me39/out.txt

       imput :
      306	305,952	304,683	302,265	300,219 (1)
      406	406,016	404,943	402,467	400,224 (2)
      506	506,038	505,182	502,518	500,878 (3)
      606	605,983	605,304	602,749	601,037 (4)
      706	705,992	705,459	703,378	701,678 (5)
      806	806,027	805,31	803,212	801,897 (6)
      906	906,015	905,542	903,513	902,139 (7)->jump
      306	306,008	304,977	302,45	299,846 (8)<-
      406	406,009	404,961	402,483	400,361 (9)
      506	506,003	505,208	502,728	500,684 (10)
      606	605,999	605,447	603,032	601,458 (11)
      706	706,012	705,41	703,335	702,125 (12)
      806	806,02	805,462	803,338	801,901 (13)
      906	906,014	905,25	903,571	901,725 (14)
    desired output:
    this should be the first group:
    100	100,064	100,26	100,202	100,005 (row2-row1)
    200 200,086	200,499	200,253	200,659 (row3-row1)
    300 300,031	300,621	300,484	300,818 (row4-row1)
    400 400,04	400,776	401,113	401,459 (row5-row1)
    500 500,075	500,627	500,947	501,678 (row6-row1)
    600 600,063	600,859	601,248	601,92  (row7-row1)->jump
    100	100,001	99,984	100,033	100,515 (row9-row8)-<
    200	199,995	200,231	200,278	200,838 (row10-row8)
    300	299,991	300,47	300,582	301,612 (row11-row8)
    400	400,004	400,433	400,885	402,279 (row12-row8)
    500	500,012	500,485	500,888	502,055 (row13-row8)
    600	600,006	600,273	601,121	601,879 (row14-row8)

the calculations, start substracting from the second row the first one. then, the from the third to the first one. as you can see in the output. It is like absolute references in Excel, but, changing this reference to the smallest value of the first columm. This first columnn is not used in the calculations but I think is useful to index the arrays. having the start in the smallest value, and ending the loop in the biggest value found in the columm/array.It would be good if it can be applied for more columms and more rows. I do not know if It is possible. Any help is welcome. I think the code to write , is quite similar as one is shown here.

Thank you very much for your help. and I hope your reply.

0 Comments

Miguelcm90

Products

No products are associated with this question.

1 Answer

Answer by Andrei Bobrov on 9 Sep 2013
Edited by Andrei Bobrov on 9 Sep 2013
Accepted answer
m = dlmread('path_your_out.txt');
t=[true;diff(m(:,1)) < 0];
out = m-n(cumsum(t),:);
out = out(any(out,2),:);

or

m = dlmread('path_your_out.txt');
s = size(m,2);
n = find(diff(m(:,1))<0,1,'first');
m1 = reshape(m,n,[],s);
m2 = bsxfun(@minus,m1(2:end,:,:),m1(1,:,:));
out = reshape(m2,[],s);

0 Comments

Andrei Bobrov

Contact us