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

Thread Subject:
matrix manipulation difficulties

Subject: matrix manipulation difficulties

From: antonio ferreira

Date: 6 Dec, 2010 18:12:05

Message: 1 of 11

Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:

m =[ 1 0 0 0 0 0 1
        5 0 0 0 0 0 3
        7 6 0 0 0 4 0
        7 7 5 0 3 7 0
        1 1 1 1 1 1 1
 ];
 r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
index_max = find(r==max(r)); %index associated to the max value
dif = transpose(zeros(1,index_max-1));
for i=1:index_max-1
dif(i)= m(i)- m(i,index_max+(index_max-i));
 end

The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards

Subject: matrix manipulation difficulties

From: Sean de

Date: 6 Dec, 2010 18:54:05

Message: 2 of 11

"antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idj91l$nti$1@fred.mathworks.com>...
> Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:
>
> m =[ 1 0 0 0 0 0 1
> 5 0 0 0 0 0 3
> 7 6 0 0 0 4 0
> 7 7 5 0 3 7 0
> 1 1 1 1 1 1 1
> ];
> r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
> index_max = find(r==max(r)); %index associated to the max value
> dif = transpose(zeros(1,index_max-1));
> for i=1:index_max-1
> dif(i)= m(i)- m(i,index_max+(index_max-i));
> end
>
> The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards

Do you mean [0; 6-3; 5-4...]?

Otherwise I can't identify the pattern you're looking for. If this is what you want:
m = cumsum(m,1);
m(~m) = nan;
[v, idx] = min(m,[],1);
Your_answer = (v-idx).';

Subject: matrix manipulation difficulties

From: Sean de

Date: 6 Dec, 2010 19:03:21

Message: 3 of 11

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjbgd$8m9$1@fred.mathworks.com>...
> "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idj91l$nti$1@fred.mathworks.com>...
> > Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:
> >
> > m =[ 1 0 0 0 0 0 1
> > 5 0 0 0 0 0 3
> > 7 6 0 0 0 4 0
> > 7 7 5 0 3 7 0
> > 1 1 1 1 1 1 1
> > ];
> > r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
> > index_max = find(r==max(r)); %index associated to the max value
> > dif = transpose(zeros(1,index_max-1));
> > for i=1:index_max-1
> > dif(i)= m(i)- m(i,index_max+(index_max-i));
> > end
> >
> > The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards
>
> Do you mean [0; 6-3; 5-4...]?
>
> Otherwise I can't identify the pattern you're looking for. If this is what you want:
> m = cumsum(m,1);
> m(~m) = nan;
> [v, idx] = min(m,[],1);
> Your_answer = (v-idx).';

Doh! This will fail if you have negative values. To account for negatives:
m =[ 1 0 0 0 0 0 1
   5 0 0 0 0 0 3
   7 6 0 0 0 4 0
   7 -8 5 0 3 7 0
   1 1 1 -2 1 1 1];

m2 = cumsum(abs(m),1);
m2(~m2) = nan;
[junk, idx] = min(m2,[],1);
Your_answer = (m(sub2ind(size(m),idx,1:size(m,2)))-idx).'

Subject: matrix manipulation difficulties

From: antonio ferreira

Date: 6 Dec, 2010 19:17:21

Message: 4 of 11

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjbgd$8m9$1@fred.mathworks.com>...
> "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idj91l$nti$1@fred.mathworks.com>...
> > Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:
> >
> > m =[ 1 0 0 0 0 0 1
> > 5 0 0 0 0 0 3
> > 7 6 0 0 0 4 0
> > 7 7 5 0 3 7 0
> > 1 1 1 1 1 1 1
> > ];
> > r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
> > index_max = find(r==max(r)); %index associated to the max value
> > dif = transpose(zeros(1,index_max-1));
> > for i=1:index_max-1
> > dif(i)= m(i)- m(i,index_max+(index_max-i));
> > end
> >
> > The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards
>
> Do you mean [0; 6-3; 5-4...]?
>
> Otherwise I can't identify the pattern you're looking for. If this is what you want:
> m = cumsum(m,1);
> m(~m) = nan;
> [v, idx] = min(m,[],1);
> Your_answer = (v-idx).';

Hi.Thanks a lot for your suggestion but your solution doesn´t apply to my case. By the way, many sorry if my explanation was not very clear. The pattern that I am looking for is this: if you take a look on my m matrix (column by column) in the first column the first element that has a non zero value is 1, in the second 6, in the third 5, in the fourth 1, in the fifth 3 in the sixth 4 and finally 1. In this context i would like to the following calculation (ignoring the middle column):
first column-last column=1-1;
2 column-6 column= 6-4;
3 column-5 column=5-3;
Do you have any other suggestion?best regards

Subject: matrix manipulation difficulties

From: Sean de

Date: 6 Dec, 2010 19:27:21

Message: 5 of 11

"antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idjcs1$9kb$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjbgd$8m9$1@fred.mathworks.com>...
> > "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idj91l$nti$1@fred.mathworks.com>...
> > > Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:
> > >
> > > m =[ 1 0 0 0 0 0 1
> > > 5 0 0 0 0 0 3
> > > 7 6 0 0 0 4 0
> > > 7 7 5 0 3 7 0
> > > 1 1 1 1 1 1 1
> > > ];
> > > r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
> > > index_max = find(r==max(r)); %index associated to the max value
> > > dif = transpose(zeros(1,index_max-1));
> > > for i=1:index_max-1
> > > dif(i)= m(i)- m(i,index_max+(index_max-i));
> > > end
> > >
> > > The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards
> >
> > Do you mean [0; 6-3; 5-4...]?
> >
> > Otherwise I can't identify the pattern you're looking for. If this is what you want:
> > m = cumsum(m,1);
> > m(~m) = nan;
> > [v, idx] = min(m,[],1);
> > Your_answer = (v-idx).';
>
> Hi.Thanks a lot for your suggestion but your solution doesn´t apply to my case. By the way, many sorry if my explanation was not very clear. The pattern that I am looking for is this: if you take a look on my m matrix (column by column) in the first column the first element that has a non zero value is 1, in the second 6, in the third 5, in the fourth 1, in the fifth 3 in the sixth 4 and finally 1. In this context i would like to the following calculation (ignoring the middle column):
> first column-last column=1-1;
> 2 column-6 column= 6-4;
> 3 column-5 column=5-3;
> Do you have any other suggestion?best regards

%v from above
(v(1:ceil(numel(v)/2)) - v(end:-1:ceil(numel(v)/2))).'

%or (v from above)
v = v - fliplr(v);
v(1:ceil(numel(v)/2)).'

Subject: matrix manipulation difficulties

From: antonio ferreira

Date: 7 Dec, 2010 11:05:07

Message: 6 of 11

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjdep$hjq$1@fred.mathworks.com>...
> "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idjcs1$9kb$1@fred.mathworks.com>...
> > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjbgd$8m9$1@fred.mathworks.com>...
> > > "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idj91l$nti$1@fred.mathworks.com>...
> > > > Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:
> > > >
> > > > m =[ 1 0 0 0 0 0 1
> > > > 5 0 0 0 0 0 3
> > > > 7 6 0 0 0 4 0
> > > > 7 7 5 0 3 7 0
> > > > 1 1 1 1 1 1 1
> > > > ];
> > > > r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
> > > > index_max = find(r==max(r)); %index associated to the max value
> > > > dif = transpose(zeros(1,index_max-1));
> > > > for i=1:index_max-1
> > > > dif(i)= m(i)- m(i,index_max+(index_max-i));
> > > > end
> > > >
> > > > The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards
> > >
> > > Do you mean [0; 6-3; 5-4...]?
> > >
> > > Otherwise I can't identify the pattern you're looking for. If this is what you want:
> > > m = cumsum(m,1);
> > > m(~m) = nan;
> > > [v, idx] = min(m,[],1);
> > > Your_answer = (v-idx).';
> >
> > Hi.Thanks a lot for your suggestion but your solution doesn´t apply to my case. By the way, many sorry if my explanation was not very clear. The pattern that I am looking for is this: if you take a look on my m matrix (column by column) in the first column the first element that has a non zero value is 1, in the second 6, in the third 5, in the fourth 1, in the fifth 3 in the sixth 4 and finally 1. In this context i would like to the following calculation (ignoring the middle column):
> > first column-last column=1-1;
> > 2 column-6 column= 6-4;
> > 3 column-5 column=5-3;
> > Do you have any other suggestion?best regards
>
> %v from above
> (v(1:ceil(numel(v)/2)) - v(end:-1:ceil(numel(v)/2))).'
>
> %or (v from above)
> v = v - fliplr(v);
> v(1:ceil(numel(v)/2)).'

Dear Sir thanks a lot for your helpful suggestions but I am still having some troubles. How can I have a matrix [v, idx] = [ 1 6 5 1 3 4 1]. You are summing the elements for each column (m = cumsum(m,1)) and I don´t want to do that. I simply want to see the first element where i have a value different from zero (in each column) . If i write r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)) I have the index associated to this value but I am not being able to associate the corresponding value. Regards

Subject: matrix manipulation difficulties

From: antonio ferreira

Date: 7 Dec, 2010 14:27:04

Message: 7 of 11

"antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idl4d3$s3v$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjdep$hjq$1@fred.mathworks.com>...
> > "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idjcs1$9kb$1@fred.mathworks.com>...
> > > "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idjbgd$8m9$1@fred.mathworks.com>...
> > > > "antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idj91l$nti$1@fred.mathworks.com>...
> > > > > Hi. This post is related to a post that I have previously posted today morning, but since I wasn`t able to find a solution I am now trying and working in a much easier example. Basically I have a matrix m and I want to identify the first element different from 0 in each column and finally subtract the corresponding values. This is the code that I have until now:
> > > > >
> > > > > m =[ 1 0 0 0 0 0 1
> > > > > 5 0 0 0 0 0 3
> > > > > 7 6 0 0 0 4 0
> > > > > 7 7 5 0 3 7 0
> > > > > 1 1 1 1 1 1 1
> > > > > ];
> > > > > r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)); %first element different from 0 column by column
> > > > > index_max = find(r==max(r)); %index associated to the max value
> > > > > dif = transpose(zeros(1,index_max-1));
> > > > > for i=1:index_max-1
> > > > > dif(i)= m(i)- m(i,index_max+(index_max-i));
> > > > > end
> > > > >
> > > > > The final result should be [0;6-4;5-3], but it´s not giving that because I am having some difficulties in defining dif(i). Could anyone give me a hint please?Regards
> > > >
> > > > Do you mean [0; 6-3; 5-4...]?
> > > >
> > > > Otherwise I can't identify the pattern you're looking for. If this is what you want:
> > > > m = cumsum(m,1);
> > > > m(~m) = nan;
> > > > [v, idx] = min(m,[],1);
> > > > Your_answer = (v-idx).';
> > >
> > > Hi.Thanks a lot for your suggestion but your solution doesn´t apply to my case. By the way, many sorry if my explanation was not very clear. The pattern that I am looking for is this: if you take a look on my m matrix (column by column) in the first column the first element that has a non zero value is 1, in the second 6, in the third 5, in the fourth 1, in the fifth 3 in the sixth 4 and finally 1. In this context i would like to the following calculation (ignoring the middle column):
> > > first column-last column=1-1;
> > > 2 column-6 column= 6-4;
> > > 3 column-5 column=5-3;
> > > Do you have any other suggestion?best regards
> >
> > %v from above
> > (v(1:ceil(numel(v)/2)) - v(end:-1:ceil(numel(v)/2))).'
> >
> > %or (v from above)
> > v = v - fliplr(v);
> > v(1:ceil(numel(v)/2)).'
>
> Dear Sir thanks a lot for your helpful suggestions but I am still having some troubles. How can I have a matrix [v, idx] = [ 1 6 5 1 3 4 1]. You are summing the elements for each column (m = cumsum(m,1)) and I don´t want to do that. I simply want to see the first element where i have a value different from zero (in each column) . If i write r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)) I have the index associated to this value but I am not being able to associate the corresponding value. Regards

Finally the solution:

m =[ 1 0 0 0 0 0 1
        5 0 0 0 0 0 3
        7 6 0 0 0 4 0
        7 7 5 0 3 7 0
        1 1 1 1 1 1 1
 ];
[r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2);
(v(1:floor(numel(v)/2)) - v(end:-1:floor(numel(v)/2+2))).'
Regards

Subject: matrix manipulation difficulties

From: Jan Simon

Date: 7 Dec, 2010 14:46:05

Message: 8 of 11

Dear Antonio,

> [r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2);

I assume you mean:
  [r,c,v]=arrayfun(@(x) find(m(:,x), 1, 'first'),1:size(m,2);
with "1" instead of "~0" -- the result is equal, but "~0" might be confusing.

Kind regards, Jan

Subject: matrix manipulation difficulties

From: Sean de

Date: 7 Dec, 2010 15:21:03

Message: 9 of 11


> > Dear Sir thanks a lot for your helpful suggestions but I am still having some troubles. How can I have a matrix [v, idx] = [ 1 6 5 1 3 4 1]. You are summing the elements for each column (m = cumsum(m,1)) and I don´t want to do that. I simply want to see the first element where i have a value different from zero (in each column) . If i write r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)) I have the index associated to this value but I am not being able to associate the corresponding value. Regards
>
> Finally the solution:
>
> m =[ 1 0 0 0 0 0 1
> 5 0 0 0 0 0 3
> 7 6 0 0 0 4 0
> 7 7 5 0 3 7 0
> 1 1 1 1 1 1 1
> ];
> [r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2);
> (v(1:floor(numel(v)/2)) - v(end:-1:floor(numel(v)/2+2))).'
> Regards


The cumsum of a bunch of zeros is zero and thus the min will be the first non-zero element. My solution is much faster (22x) than yours. Store this in an mfile and run it if you wish.

%%%%
m =[ 1 0 0 0 0 0 1
   5 0 0 0 0 0 3
   7 6 0 0 0 4 0
   7 -8 5 0 3 7 0
   1 1 1 -2 1 1 1];

t1 = 0;
t2 = 0;
for ii = 1:100
tic
m2 = cumsum(abs(m),1);
m2(~m2) = nan;
[v, idx] = min(m2,[],1);
%Your_answer = (m(sub2ind(size(m),idx,1:size(m,2)))-idx).';
v1 = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
t1 = t1+toc;

tic
[r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2));

v2 = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
t2 = t2+toc;
end
isequal(v1,v2)
t1
t2

Subject: matrix manipulation difficulties

From: antonio ferreira

Date: 7 Dec, 2010 17:19:21

Message: 10 of 11

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idljcv$jmh$1@fred.mathworks.com>...
>
> > > Dear Sir thanks a lot for your helpful suggestions but I am still having some troubles. How can I have a matrix [v, idx] = [ 1 6 5 1 3 4 1]. You are summing the elements for each column (m = cumsum(m,1)) and I don´t want to do that. I simply want to see the first element where i have a value different from zero (in each column) . If i write r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)) I have the index associated to this value but I am not being able to associate the corresponding value. Regards
> >
> > Finally the solution:
> >
> > m =[ 1 0 0 0 0 0 1
> > 5 0 0 0 0 0 3
> > 7 6 0 0 0 4 0
> > 7 7 5 0 3 7 0
> > 1 1 1 1 1 1 1
> > ];
> > [r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2);
> > (v(1:floor(numel(v)/2)) - v(end:-1:floor(numel(v)/2+2))).'
> > Regards
>
>
> The cumsum of a bunch of zeros is zero and thus the min will be the first non-zero element. My solution is much faster (22x) than yours. Store this in an mfile and run it if you wish.
>
> %%%%
> m =[ 1 0 0 0 0 0 1
> 5 0 0 0 0 0 3
> 7 6 0 0 0 4 0
> 7 -8 5 0 3 7 0
> 1 1 1 -2 1 1 1];
>
> t1 = 0;
> t2 = 0;
> for ii = 1:100
> tic
> m2 = cumsum(abs(m),1);
> m2(~m2) = nan;
> [v, idx] = min(m2,[],1);
> %Your_answer = (m(sub2ind(size(m),idx,1:size(m,2)))-idx).';
> v1 = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
> t1 = t1+toc;
>
> tic
> [r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2));
>
> v2 = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
> t2 = t2+toc;
> end
> isequal(v1,v2)
> t1
> t2

Right, much more faster. Just one more question please. My "true" matrix m consists of several "blocks" of 7 columns.

m =[ 1 0 0 0 0 0 1 9 10 9 0 0 0 0 0 1
        5 0 0 0 0 0 3 10 8 5 0 0 0 0 0 3
        7 6 0 0 0 4 0 8 7 7 9 0 0 0 4 0
        7 -8 5 0 3 7 0 6 7 7 -8 7 0 3 7 0
        1 1 1 -2 1 1 1 6 7 1 1 1 -2 1 1 1];
Basically I need to consider the first 7 columns, ignore the next two (the ones which start by 9 and 10) and then consider the 7 last columns. For these "two blocks" of 7 columns Ii need to perform the calculations that are described above. Is there any simple way of doing this?Best Regards

Subject: matrix manipulation difficulties

From: Sean de

Date: 7 Dec, 2010 17:42:05

Message: 11 of 11

"antonio ferreira" <edgar.acferreira@gmail.com> wrote in message <idlqap$rrb$1@fred.mathworks.com>...
> "Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <idljcv$jmh$1@fred.mathworks.com>...
> >
> > > > Dear Sir thanks a lot for your helpful suggestions but I am still having some troubles. How can I have a matrix [v, idx] = [ 1 6 5 1 3 4 1]. You are summing the elements for each column (m = cumsum(m,1)) and I don´t want to do that. I simply want to see the first element where i have a value different from zero (in each column) . If i write r=arrayfun(@(t) find(m(:,t),~0,'first'),1:size(m,2)) I have the index associated to this value but I am not being able to associate the corresponding value. Regards
> > >
> > > Finally the solution:
> > >
> > > m =[ 1 0 0 0 0 0 1
> > > 5 0 0 0 0 0 3
> > > 7 6 0 0 0 4 0
> > > 7 7 5 0 3 7 0
> > > 1 1 1 1 1 1 1
> > > ];
> > > [r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2);
> > > (v(1:floor(numel(v)/2)) - v(end:-1:floor(numel(v)/2+2))).'
> > > Regards
> >
> >
> > The cumsum of a bunch of zeros is zero and thus the min will be the first non-zero element. My solution is much faster (22x) than yours. Store this in an mfile and run it if you wish.
> >
> > %%%%
> > m =[ 1 0 0 0 0 0 1
> > 5 0 0 0 0 0 3
> > 7 6 0 0 0 4 0
> > 7 -8 5 0 3 7 0
> > 1 1 1 -2 1 1 1];
> >
> > t1 = 0;
> > t2 = 0;
> > for ii = 1:100
> > tic
> > m2 = cumsum(abs(m),1);
> > m2(~m2) = nan;
> > [v, idx] = min(m2,[],1);
> > %Your_answer = (m(sub2ind(size(m),idx,1:size(m,2)))-idx).';
> > v1 = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
> > t1 = t1+toc;
> >
> > tic
> > [r,c,v]=arrayfun(@(x) find(m(:,x),~0,'first'),1:size(m,2));
> >
> > v2 = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
> > t2 = t2+toc;
> > end
> > isequal(v1,v2)
> > t1
> > t2
>
> Right, much more faster. Just one more question please. My "true" matrix m consists of several "blocks" of 7 columns.
>
> m =[ 1 0 0 0 0 0 1 9 10 9 0 0 0 0 0 1
> 5 0 0 0 0 0 3 10 8 5 0 0 0 0 0 3
> 7 6 0 0 0 4 0 8 7 7 9 0 0 0 4 0
> 7 -8 5 0 3 7 0 6 7 7 -8 7 0 3 7 0
> 1 1 1 -2 1 1 1 6 7 1 1 1 -2 1 1 1];
> Basically I need to consider the first 7 columns, ignore the next two (the ones which start by 9 and 10) and then consider the 7 last columns. For these "two blocks" of 7 columns Ii need to perform the calculations that are described above. Is there any simple way of doing this?Best Regards

The simplest way would be with a for loop
%%%%
m =[ 1 0 0 0 0 0 1
   5 0 0 0 0 0 3
   7 6 0 0 0 4 0
   7 -8 5 0 3 7 0
   1 1 1 -2 1 1 1];
m = [m [randi(10,5,2)] m];

result_store = zeros(3,length(1:9:size(m,2)));
col = 0;
for ii = 1:9:size(m,2)
col = col+1;
    m2 = cumsum(abs(m(:,ii:ii+6)),1);
    m2(~m2) = nan;
    [v, idx] = min(m2,[],1);
    result_store(:,col) = (v(1:floor(numel(v)/2)) - v(end:-1:ceil(numel(v)/2)+1)).';
end

%%%
result_store will be a 3xn matrix with each column corresponding to the nth extracted block

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us