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

re arrange/shape matrix according to the position of a index value in array.

Asked by Miguelcm90 on 8 Sep 2013

Dear friends, many thanks for helping me, it was succesfull.

I would like to help me in this task, having the next file as example, I will try to explain with only a reduced number of values of the file.

https://www.dropbox.com/s/y3ehqalm3cxmzuj/x.txt

Below is what I have to start, I would need something what is able to do what is shown in "desired output"

 imput: 
      306,000	305,952	304,683	302,265	300,219
      306,000	306,008	304,977	302,450	299,846
      306,000	305,962	304,840	302,413	299,812
      306,000	306,051	305,051	302,396	299,877
      306,000	306,003	304,891	302,163	300,017
      306,000	306,004	305,002	301,997	300,186
      406,000	406,016	404,943	402,467	400,224
      406,000	406,009	404,961	402,483	400,361
      406,000	406,014	404,852	402,547	400,287
      406,000	406,043	404,990	402,520	400,372
      406,000	406,048	405,105	402,425	400,817
      406,000	406,034	404,928	402,425	400,623
      506,000	506,038	505,182	502,518	500,878
      506,000	506,003	505,208	502,728	500,684
      506,000	505,971	505,110	502,841	501,118
      506,000	505,979	505,083	502,644	500,922
      506,000	505,985	505,065	502,942	500,955
      506,000	506,062	505,067	502,818	501,280
    desired output:
       (first 3 rows of all groups of same index)
      306,000	305,952	304,683	302,265	300,219 
      406,000	406,016	404,943	402,467	400,224
      506,000	506,003	505,208	502,728	500,684
      (second 3 rows of all groups of same index)
      306,000	306,008	304,977	302,450	299,846 
      406,000	406,009	404,961	402,483	400,361
      506,000	506,003	505,208	502,728	500,684 
      (third 3 rows of all groups of same index
      306,000	305,962	304,840	302,413	299,812 
      406,000	406,014	404,852	402,547	400,287
      506,000	505,971	505,110	502,841	501,118
      fourth 3 rows of all groups of same index 
      306,000	306,051	305,051	302,396	299,877  
      406,000	406,043	404,990	402,520	400,372
      506,000	505,979	505,083	502,644	500,922
      fifth... 
      306,000	306,003	304,891	302,163	300,017  
      406,000	406,048	405,105	402,425	400,817
      506,000	505,985	505,065	502,942	500,955
      sitxh...
      306,000	306,004	305,002	301,997	300,186  
      406,000	406,034	404,928	402,425	400,623
      506,000	506,062	505,067	502,818	501,280

The desired output would be if, for instance, in the original file I have 234 indexed in groups of 6 rows, I would like to obtain at the end matrix of 39x5 , and the number of matrix is the length, or the amount of rows that I have per group.

I hope to have been clear. And, anyway, many thanks for your help. It was very useful for me, and I am doing little steps to learn how to program.

I know my question is tricky, but once I have the way, I will understand how it works.

Best Regards and heartfelt thanks

Miguel

1 Comment

Andrei Bobrov on 8 Sep 2013

repeated question

Miguelcm90

Products

No products are associated with this question.

2 Answers

Answer by Azzi Abdelmalek on 8 Sep 2013
Edited by Azzi Abdelmalek on 9 Sep 2013
Accepted answer

% If your_array is your cell array

[a,b]=unique(your_array(:,1),'stable');
n=size(your_array,1)/numel(a);
zz=reshape(your_array',size(your_array,2)*n,[])'
zz1=reshape(zz,size(zz,1),[],n);
out= cell2mat(arrayfun(@(x) zz1(:,:,x),(1:n)','un',0))

%or

[a,b]=unique(your_array(:,1),'stable');
n=size(your_array,1)/numel(a);
zz=reshape(your_array',size(your_array,2)*n,[])';
zz1=reshape(zz,size(zz,1),[],n);
q=permute(zz1,[2 1 3]);
out1=q(:,:)';

4 Comments

Miguelcm90 on 9 Sep 2013

Dear Friend, your help works well,

Now, 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.

Andrei Bobrov on 9 Sep 2013

see ADD2 part in my answer

Azzi Abdelmalek on 9 Sep 2013
[a,b]=unique(your_array(:,1),'stable');
n=size(your_array,1)/numel(a);
zz=reshape(your_array',size(your_array,2)*n,[])';
zz1=reshape(zz,size(zz,1),[],n);
out=cell2mat(arrayfun(@(x) bsxfun(@minus,zz1(2:end,:,x),zz1(1,:,x)),(1:n)','un',0))
Azzi Abdelmalek
Answer by Andrei Bobrov on 8 Sep 2013
Edited by Andrei Bobrov on 9 Sep 2013
z=[306.000 305.952	304.683	302.265	300.219
306.000	306.008	304.977	302.450	299.846
306.000	305.962	304.840	302.413	299.812
306.000	306.051	305.051	302.396	299.877
306.000	306.003	304.891	302.163	300.017
306.000	306.004	305.002	301.997	300.186
406.000	406.016	404.943	402.467	400.224
406.000	406.009	404.961	402.483	400.361
406.000	406.014	404.852	402.547	400.287
406.000	406.043	404.990	402.520	400.372
406.000	406.048	405.105	402.425	400.817
406.000	406.034	404.928	402.425	400.623
506.000	506.038	505.182	502.518	500.878
506.000	506.003	505.208	502.728	500.684
506.000	505.971	505.110	502.841	501.118
506.000	505.979	505.083	502.644	500.922
506.000	505.985	505.065	502.942	500.955
506.000	506.062	505.067	502.818	501.280];
out = permute(reshape(z,[],3,size(z,2)),[2 3 1]);

ADD

s = size(z);
n = s(1)/numel(unique(z(:,1)));
out = reshape(permute(reshape(z,n,[],s(2)),[2 1 3]),s);

ADD 2

m = dlmread('path_your_out.txt');
t=[true;diff(m(:,1)) < 0];
out = m-n(cumsum(t),:);
out = out(any(out,2),:);

0 Comments

Andrei Bobrov

Contact us