Compare matrices built with repelem and repmat

23 vues (au cours des 30 derniers jours)
Luca Gagliardone
Luca Gagliardone le 5 Août 2017
Commenté : Luca Gagliardone le 5 Août 2017
I have an array a of size ZxW.
Z = 20;
W = 30;
A = 40; %will be used below
size(a)
20 30
Then I apply to a two different transformations, and then after those I delete a and I cannot go back to it.
First transformation:
b = repelem(a(:,1),A,A);
Second transformation:
c = repmat(a,[1,1,A,A]);
d = c(:,1,:,:);
After those transformation and deleting a (which cannot be used for the following), I want to compare d and b using
assert( isequal(b,f) )
Where f is a transformation of d that makes the assertion true.
My first idea was a simple reshape:
f = reshape(squeeze(d),[Z*A,A]);
Which does not work as repelem and repmat move entries differently. How can I do this?
Thanks for the attention.
Sincerely
Luca
  2 commentaires
Jan
Jan le 5 Août 2017
What is the difference to your question https://www.mathworks.com/matlabcentral/answers/351565-from-kron-to-repmat-and-reshape? Please do not post two question for one problem, but edit the original question and answer questions for clarifications. Thanks.
Luca Gagliardone
Luca Gagliardone le 5 Août 2017
The original question in your link was not well posed and your answer was perfect for the question.
I thought it could have been a good idea for clarity to pose a different question, but you are right, I probably should have edited. Sorry for that, I will reply at your link. Thanks.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 5 Août 2017
Modifié(e) : Jan le 5 Août 2017
The transformation
c = repmat(a, [1,1,A,A]);
d = c(:,1,:,:);
is an indirection to reproduce b.
Z = 2;
W = 3;
A = 4; % Smaller to check the output manually
a = rand(Z, W);
b = repelem(a(:,1),A,A);
c = reshape(repmat(a(:,1).', [A, A]), Z*A, A);
isequal(b, c)
But if you really want
c = repmat(a, [1,1,A,A]);
d = c(:,1,:,:);
Then:
f = reshape(permute(d, [3,1,4,2]), [Z*A,A]);
isequal(b, f) % 1: equal
  1 commentaire
Luca Gagliardone
Luca Gagliardone le 5 Août 2017
Thanks Jan, this is perfect.
Luca

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by