I have the following matrix:
[ 1 5
4 9]
What's the fastest way, and without a for loop, to expand it to:
[ 1 1 5 5
1 1 5 5
4 4 9 9
4 4 9 9]
or to
[ 1 1 1 5 5 5
1 1 1 5 5 5
1 1 1 5 5 5
4 4 4 9 9 9
4 4 4 9 9 9
4 4 4 9 9 9]
?
Thanks in advance!
A=[1 4; 4 9]
kron(A,ones(3))
% Bruno
[m n]=size(A)
mdup=4; ndup=3;
AX=repmat(A,[1 1 mdup ndup]);
AX=permute(AX,[3 1 4 2]);
AX=reshape(AX,m*mdup,n*ndup)
% Bruno
For sure KRON is more readable.
Up to you
Bruno
% Data
A= [ 1 5;4 9]
% Engine
idx = cumsum(ones(2),2)
B = A(idx,idx)
B = A(zeros(2,1)+(1:end),zeros(2,1)+(1:end))
% Bruno
[m n]=size(A)
mdup=4; ndup=3;
vx=ceil((1:m*mdup)/mdup);
vy=ceil((1:n*ndup)/ndup);
A(vx,vy)
Well, the weird characters after vx and vy are supposed to be '=ceil'
vx=ceil((1:m*mdup)/mdup);
vy=ceil((1:n*ndup)/ndup);
Still not showing up, I will try. Do you mean ceil?
vx = ceil((1:m*mdup)/mdup);
vy = ceil((1:n*ndup)/ndup);
> Still not showing up, I will try. Do you mean ceil?
>
> vx = ceil((1:m*mdup)/mdup);
> vy = ceil((1:n*ndup)/ndup);
Yes. for some reason '= ceil' without the space generates these weird characters...
I like this method. It's simple. But, how would you do this for a 3x3 matrix or a 4x4 matrx?
My method is basically Matt Fig's idea, but generalized to arbitrary matrix size and arbitrary upsampling
[m n]=size(A)
mdup=4; ndup=3;
vx = ceil((1:m*mdup)/mdup);
vy = ceil((1:n*ndup)/ndup);
A(vx,vy)
Matt's method is more easily generalizable for arbitrary expansion in each direction. Nevertheless, here is a generalization of the method behind my example.
N = 3; % A is NxN
E = 4; % The expansion number.
A = round(rand(N)*10);
idx = cumsum(ones(E,N),2);
B = A(idx,idx)
matt - don't get sidetracked, don't procrastinate! you should be improving your FINDSUBMAT...
as ever with a :-)
urs
The watchful eye of Urs! ;-).
Bruno
matt: bruno - the devil in disguise!...
www.youtube.com/watch?v=m3_Q96eJr1k
[lol]
urs