How to clump/consolidate values together using the mean function
1 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
So to explain the title a little more, say I have a 8x8 matrix all of different values, how would I make another 8x8 matrix but just with 4 values in each quadrant?
I have written the following code in an attempt to do this
cw = 4; %cell width
a = [1:8];
b=[1:8]';
c=a.*b;
lp = [1 5]; %length position
mat=ones(8,8); %temporary matrix
for i = 1:numel(lp)
for j = 1:numel(lp)
mat(lp(i):lp(i)+cw-1,lp(i):lp(i)+cw-1)=mean(c(lp(i):lp(i)+cw-1,lp(i):lp(i)+cw-1),"all");
end
end
it partially achieves the desired outcome, calcualting the upper left quadrant as 6.25 and the lower right value as 42.25 but you can see the other quadrants remain as the pre-defined 1.
I'm sure there is an easier way to do this, I probably just don't know the name for what I'm trying to do, but any help would be really appreciated.
Thank you all
0 个评论
回答(1 个)
Alan Stevens
2021-1-15
编辑:Alan Stevens
2021-1-15
Here's one way
cw = 4; %cell width
a = 1:8;
b = (1:8)';
c = a.*b;
mat=ones(8,8); %temporary matrix
avfn = @(m) mean(m,'ALL');
p = 1:cw; q = cw+1:2*cw;
mat(p,p) = avfn(c(p,p));
mat(p,q) = avfn(c(p,q));
mat(q,p) = avfn(c(q,p));
mat(q,q) = avfn(c(q,q));
2 个评论
Alan Stevens
2021-1-18
I think the following does it, though you should check it carefully:
n = 16; % n x n matrix
cw = 4; %cell width
a = 1:n;
b = (1:n)';
c = a.*b;
nc = n/cw; % nc x nc cells (Assumes nc is integer)
mat=ones(n); %temporary matrix
avfn = @(m) mean(m,'ALL');
s = 1;
for i = 1:nc
f = s-1+cw;
p(i,:) = s:f;
s = f+1;
end
for i = 1:nc
for j = 1:nc
mat(p(i,:),p(j,:)) = avfn(c(p(i,:),p(j,:)));
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!