sharing of numbers in the matrix
2 次查看(过去 30 天)
显示 更早的评论
If
A=[0.1 0 0.2 0 0 0 0 0;0 0 0 0.3 0.4 0 0 0;0 0.5 0 0 0 0 0 0.6;0 0 0 0 0 0.7 0.8 0]
I want to have
B=[0.1 0 0.2 0.3 0.4 0 0 0;0.1 0 0.2 0.3 0.4 0 0 0;0 0.5 0 0 0 0 0.7 0.8 0.6;0 0.5 0 0 0 0 0.7 0.8 0.6]
could anyone tell me how to get it?
6 个评论
回答(2 个)
Stephen23
2017-12-15
编辑:Stephen23
2017-12-15
Solution to your original question:
>> reshape(repmat(max(reshape(A,2,[])),2,1),size(A))
ans =
0.10000 0.00000 0.20000 0.30000 0.40000 0.00000 0.00000 0.00000
0.10000 0.00000 0.20000 0.30000 0.40000 0.00000 0.00000 0.00000
0.00000 0.50000 0.00000 0.00000 0.00000 0.70000 0.80000 0.60000
0.00000 0.50000 0.00000 0.00000 0.00000 0.70000 0.80000 0.60000
Solution to your later comment:
>> repmat(reshape(max(reshape(A,2,[])),2,[]),2,1)
ans =
0.10000 0.00000 0.20000 0.30000 0.40000 0.00000 0.00000 0.00000
0.00000 0.50000 0.00000 0.00000 0.00000 0.70000 0.80000 0.60000
0.10000 0.00000 0.20000 0.30000 0.40000 0.00000 0.00000 0.00000
0.00000 0.50000 0.00000 0.00000 0.00000 0.70000 0.80000 0.60000
Andrei Bobrov
2017-12-15
编辑:Andrei Bobrov
2017-12-15
out = repelem(squeeze(max(permute(reshape(A,2,2,[]),[1,3,2]))),1,2)';
or
out = A;
[ii,jj] = find(A);
t = rem(ii,2);
out(sub2ind(size(out),ii - ~t + t,jj)) = A(A>0);
3 个评论
Andrei Bobrov
2017-12-15
编辑:Andrei Bobrov
2017-12-15
See Stephen's answer (first part).
B = reshape(repmat(max(reshape(A,2,2,[]),[],2),1,2),size(A));
or
[ii,jj] = find(A);
B = A;
B(sub2ind(size(A),rem(ii + 1,4)+1,jj)) = A(A>0);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!