Created
March 13, 2015 03:26
-
-
Save cwhy/d5b7a255785d508e8850 to your computer and use it in GitHub Desktop.
Matlab 3D testing vectorization
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
% Use case: | |
% we have test function m = f(x, y, z), where we vary x, y, and z to get different ms | |
% we want the result M where M(x, y, z) = f(x, y, z) | |
% but vectorized with one for loop | |
% Example: xs = 1:2 | |
% ys = 3:5 | |
% zs = 6:9 | |
[X,Y,Z] = meshgrid(xs, ys, zs); | |
test_instances = [X(:) Y(:) Z(:)]; | |
% Example 1: | |
% results = X(:) + 10*Y(:) + 100*Z(:); | |
% Example 2: | |
% test_range = size(test_instances, 1); | |
% for i = 1:test_range | |
% X(:) Y(:) Z(:) | |
% end | |
M = reshape(results, length(ys), length(xs), length(zs)) | |
M = permute(M, [2 1 3]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment