File: Magnify2DMatrix.m

package info (click to toggle)
psychtoolbox-3 3.0.9%2Bsvn2579.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 63,408 kB
  • sloc: ansic: 73,310; cpp: 11,139; objc: 3,129; sh: 1,669; python: 382; php: 272; makefile: 172; java: 113
file content (64 lines) | stat: -rw-r--r-- 2,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function destination = Magnify2DMatrix(source, scalingFactor) 
% destination = Magnify2DMatrix(sourceMatrix, scalingFactor)
%
% Magnifies a 2-D or 3-D 'sourceMatrix' by a factor specified by
% 'scalingFactor'. size of 3rd dimension will not be scaled.
%

% 10/15/06 rhh Wrote it using lots of loops.
% 11/12/06 rhh Revised it.  Denis Pelli suggested revising the code to take
%               advantage of Matlab's matrix processing abilities and David Brainard
%               showed explicitly how this could be done.
% 05/09/08 DN  generated copy instruction indices with a different method,
%               speeding up this function. Added support for 3D matrices

heightOfSource  = size(source,1);
widthOfSource   = size(source,2);
% Generate row copying instructions index.
inds                            = 1:heightOfSource;
rowCopyingInstructionsIndex     = inds(ones(scalingFactor,1),:);
rowCopyingInstructionsIndex     = rowCopyingInstructionsIndex(:);


% Generate column copying instructions index.
inds                            = 1:widthOfSource;
columnCopyingInstructionsIndex  = inds(ones(scalingFactor,1),:);
columnCopyingInstructionsIndex  = columnCopyingInstructionsIndex(:)';

% The following code uses Matlab's matrix indexing quirks to magnify the
% matrix.  It is easier to understand how it works by looking at a specific
% example:
% 
% >> n = [1 2; 3 4] % Matlab, please give me a matrix with four elements.
%
% n =
% 
%      1     2
%      3     4
% 
% >> % Matlab, please generate a new matrix by using the provided copying
% >> % instructions index.  My copying instructions index says that you
% >> % should print the first column twice, then print the second column
% >> % twice.  Thanks.
% >> m = n(:, [1 1 2 2])
% 
% m =
% 
%      1     1     2     2
%      3     3     4     4
%
% >> % Matlab, please generate a new matrix by using the provided copying
% >> % instructions index.  My copying instructions index says that you
% >> % should print the first row twice, then print the second row
% >> % twice.  Thanks.
% >> m = n([1 1 2 2], :)
% 
% m =
% 
%      1     2
%      1     2
%      3     4
%      3     4
%

destination = source(rowCopyingInstructionsIndex, columnCopyingInstructionsIndex,:);