image thumbnail


version (1.98 KB) by Noam Katz
adds dimensions to make x look like y


Updated 28 Nov 2007

View License

reindex(x,y) adds dimensions without the confusion of reshape. It makes x look like y. For example:
x = ones([1 5]);
y = ones([6 5 9]);
z = y + x; %this fails
z = y + reindex(x,y); %but this does not

Works for arbitrary dimensions, as long as all non-singleton dimensions of x are also dimension of y.

reindex is more general than bsxfun (though maybe slower), because it can do repmat and permute without much thought from user:
x = zeros([5 9]);
y = ones([3 5 1 9 2]);
z = bsxfun(@minus,y,x)); %fails because dimensions don't match
z = y-reindex(x,y); %works, z has dimension of y

Cite As

Noam Katz (2021). reindex (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2006b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!