Skip to content

Instantly share code, notes, and snippets.

@cwhy
Created March 13, 2015 03:26
Show Gist options
  • Save cwhy/d5b7a255785d508e8850 to your computer and use it in GitHub Desktop.
Save cwhy/d5b7a255785d508e8850 to your computer and use it in GitHub Desktop.
Matlab 3D testing vectorization
% 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