How to to write a code to my n*1 matrix into differnt submatrices?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Matt Fig
2011-3-3
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 个评论
Walter Roberson
2011-3-4
Z = cellfun(@(G) mean(data(G)), groups);
S = cellfun(@(G) mean(t(G)), groups);
更多回答(2 个)
Paulo Silva
2011-3-3
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Repeated Measures and MANOVA 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!