Code covered by the BSD License  

Highlights from
reindex

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 1.98 KB File ID: #17656

reindex

by Noam Katz

 

21 Nov 2007 (Updated 28 Nov 2007)

adds dimensions to make x look like y

| Watch this File

File Information
Description

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

MATLAB release MATLAB 7.3 (R2006b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
26 Nov 2007 John D'Errico

Yes, but if you use bsxfun, you gain far more! By using bsxfun, you never need to expand these arrays at all. This becomes far more efficient, both in terms of memory and speed. Finally, bsxfun comes with matlab now, so you don't even need to download anything at all.

Please login to add a comment or rating.
Updates
28 Nov 2007

Address review, clarify use of function

Tag Activity for this File
Tag Applied By Date/Time
matrices Noam Katz 22 Oct 2008 09:36:23
reindex reshape permute Noam Katz 22 Oct 2008 09:36:23

Contact us at files@mathworks.com