# How to to write a code to my n*1 matrix into differnt submatrices?

Asked by sudheer kumar reddy on 3 Mar 2011
Latest activity Commented on by sudheer kumar reddy on 4 Mar 2011

Hi guys, I have N*1 Index matrix with N rows. Where N varies for different files. My matrix consists N number of channels. I need to copy my channels into one group when difference between my channel is greater than one.I need to make differnt groups whenever differnce between the channels is greater than one. I tried to use Magic function and find function to save into different groups. When I use FIND function, I can find channels which difference are greater than one and less than one. I am unable to copy them into differnt groups.

Can anyone help me with this problem

Cheers,

Sudheer

## Products

Answer by Matt Fig on 3 Mar 2011

Can you give a small example matrix and what you expect the output to be?

It is still not clear what you want. I asked you to provide input and output. You provided input, but no output. How would you group these, in a cell array?

Here is a guess:

```A = [164
165
166
167
175
176
177
189
190
195
196];
```
```T = [1;diff(A)]==1;
t1 = [1 findstr((T).',[0 1])];
t2 = [findstr((T).',[1 0]) length(T)];
```
```H = cell(1,length(t1));
for ii = 1:length(t1)
H{ii} =  A(t1(ii):t2(ii));
end
```
```R = num2cell(A(~ismembc(A,cat(1,H{:}))));
H(end+1:end+length(R)) = R
```

.

.

EDIT

A one-liner (FWIW):

```groups = mat2cell(A,diff([0;find(diff(A) ~= 1);length(A)]),1);
```

sudheer kumar reddy on 3 Mar 2011

close all

t=NUM(:,1);
data=NUM(:,2);
figure;
clf;
hold on;
plot(t,data);
figure(5); hold on;

Z1=smooth(t,'rlowess');
Z2=smooth(data,12,'rloess');
figure(5);
plot(Z1,Z2)

K = (diff(Z2)); %to calculate the difference in voltage
K1 = abs(diff(Z2));
figure; plot(K)

ind = find(K1>0.0275); %check whther the diff is >0.02

i=1;
j=1;
C1=diff(ind);

while(~isempty(ind))

if (K1(i)<0) || ((K1(i)>0) && K1(i+1)>0)
i = i+1;
else
break;
end
X = t(ind);
Y = data(ind);

figure(5);hold on;
plot(X,Y,'r*')

A=ind;

groups = mat2cell(A,diff([0;find(diff(A) ~= 1);length(A)]),1);

Z=data(groups);
S=t(groups);
end

I am unale to get Z and S by using mat2cell function. If I can get Z and S I can use cellfun function and fing mean of my groups and plot.

Cheers

Walter Roberson on 4 Mar 2011

Z = cellfun(@(G) mean(data(G)), groups);
S = cellfun(@(G) mean(t(G)), groups);

sudheer kumar reddy on 4 Mar 2011

Thank you Walter Robertson, your tip helped me..

Cheers

Answer by Jan Simon on 3 Mar 2011

It sounds like you need HIST or HISTC, especially the 2nd output argument.

## 1 Comment

sudheer kumar reddy on 3 Mar 2011

I went through HIST. But, this only helps me to find count of my array. Where as, I need to group my data points in different groups.

Cheers

Answer by Paulo Silva on 3 Mar 2011
```a=[164 165 166 167 175 176 177 189 190 195 196]';
b=diff(a); %find differences
idx=find(b>1); %find their indexes
idx=[idx;numel(a)]; %add the last value as index
cel=cell(1,numel(idx)); %make a cell to hold the groups
sv=1; %start value
for f=1:numel(idx)
cel{f}=a(sv:idx(f)); %take each part of a into a group
sv=idx(f)+1; %make the next start value
end
bar(cellfun(@mean,cel))
```

Paulo Silva on 3 Mar 2011

Thanks Matt, your tips are always welcome :)

sudheer kumar reddy on 3 Mar 2011

Thanks for the code. In the following code I am not aware of how many a values I get. So, I am not sore about number of groups I get. If I get this I need to calculate mean between values in my group and plot.