Created
March 4, 2015 09:05
-
-
Save shaybensasson/b3fa936f16fe991cdfa9 to your computer and use it in GitHub Desktop.
reshape, repmat and bsxfun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% using reshape() to change a multi dimensional array into a smaller | |
% dimension without loosing values | |
%vector into matrix 5x2 (rowxcol) | |
A = 1:10; | |
B = reshape(A,[5,2]) | |
%reshape matrix to have 3 columns, discarding rows | |
A = magic(6); | |
B = reshape(A,[],3) | |
%% using repmat() to change a multi dimensional array into a bigger | |
A = diag([100 200 300]) | |
%{ | |
A = | |
100 0 0 | |
0 200 0 | |
0 0 300 | |
%} | |
B = repmat(A,2) %repeat matrix on 2x2: Create a 2-by-2 tiling of A | |
%{ | |
B = | |
100 0 0 100 0 0 | |
0 200 0 0 200 0 | |
0 0 300 0 0 300 | |
100 0 0 100 0 0 | |
0 200 0 0 200 0 | |
0 0 300 0 0 300 | |
%} | |
%vertical stack of row values | |
A = 1:4; | |
B = repmat(A,4,1) | |
%{ | |
B = | |
1 2 3 4 | |
1 2 3 4 | |
1 2 3 4 | |
1 2 3 4 | |
%} | |
%% 'bsxfun' is much faster | |
idx = bsxfun(@plus, (0:2)', 1:10) | |
%{ | |
idx = | |
1 2 3 4 5 6 7 8 9 10 | |
2 3 4 5 6 7 8 9 10 11 | |
3 4 5 6 7 8 9 10 11 12 | |
%} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment