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

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

 Réponse acceptée

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);

9 commentaires

For instance, I have 20 channels, which are my 20 rows.
Channels
164
165
166
167
175
176
177
189
190
195
196
Now if you analyze difference between my first 4 channels is 1 and difference between 4 and 5 is greater than one and so on the cycle continues. When the difference is greater than one I want all previous channels into one group and next comes into different group and so on.
If you need more information please do not hesitate to reply back
Cheers
Hi MattFig,
Thanks for your code. I would explain what I am doing in my project. The following values which I have give represents data points. Now when I get different groups of my data points I can calculate mean of my data points and plot them on my wave form. The only reason I need to make into different groups is, if I plot my data points I am plotting many points at a sudden change. Where I need only one point which specifies my peak. Please do not hesitate to ask any more information.
cheers
HI Matt Fig,
groups = mat2cell(A,diff([0;find(diff(A) ~= 1);length(A)]),1);
The following function is straight forward and thats what I need to make groups. Thanks for that.
In the following function A is the indes of my data points and time. Data points is in voltage. Now I need to get data points of my A in groups and calcuate mean of my data points in my groups. I tried to call data(A), t(A), but I get an error message which doesnt accept data(A), t(A). Can you please suggest me with some solution. I can provide any more furter information if you need.
Cheers
This should work cellfun(@mean,groups)
Hi Paulo Silva,
cellfun(@mean,groups)
with the following function I am able to find mean of my groups. But, I need to find mean of my data points which are represented by A. If I can get datapoints and time into groups I can use the following function and get find mean and plot.
I ll give an example,
I have got groups
group1= 162
163
164
165
group2= 170
171
172
173
these groups are index points of time and voltage. Now, I need to call my time and voltage into these groups and find maean and plot it.
Can you please give me futher advice
Cheers
time and voltage? are they stored in a cell or something? please give us one small example
close all
[NUM,TXT,RAW]=xlsread('b3br.csv');
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.
Please give me some advice.
Cheers
Z = cellfun(@(G) mean(data(G)), groups);
S = cellfun(@(G) mean(t(G)), groups);
Thank you Walter Robertson, your tip helped me..
Cheers

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 3 Mar 2011
It sounds like you need HIST or HISTC, especially the 2nd output argument.

1 commentaire

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.
Can you please give me any further advice.
please do not hesitate to ask any more information.
Cheers

Connectez-vous pour commenter.

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))

4 commentaires

This is a better solution, only calling FIND once and preserving the order.
You could get sv directly by:
idx = find(diff(A)~=1);
idx = [idx;length(A)];
sv = [1; idx(1:end-1)+1];
+1 vote
Thanks Matt, your tips are always welcome :)
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.
Can you please help me with this. Please do not hesitate to ask any more information.
cheers
cellfun(@mean,cel)

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by