File Exchange

image thumbnail

NDLINSPACE (v1.1, feb 2008)

version 1.1.0.0 (2.5 KB) by Jos (10584)
Generalized LINSPACE: linearly spaced values between mutliple points

1 Download

Updated 03 Feb 2009

View Version History

View License

NDLINSPACE - Generalized linearly spaced matrix for multiple points

R = NDLINSPACE(M1,M2, N) returns a matrix of size [size(M1) N] holding
N equally spaced points between corresponding point of M1 and M2. If N
is smaller than 2, M2 is returned. R = NDLINSPACE(M1,M2) uses N = 100.

For scalar inputs, NDLINSPACE(S1,S2,N) mimicks LINSPACE, returning
equally spaced points between the S1 and S2. Example:

ndlinspace(0, 2, 4)
% -> 0 0.67 1.33 2.00

For two M-by-1 column vectors, NDLINSPACE(V1,V2,N) returns a M-by-N
matrix in which the k-th row holds the N equally spaced numbers between
V1(k) and V2(k). Similary, for two 1-by-M row vectors, a N-by-M matrix
is returned. Example:

ndlinspace([0 ; 12 ; 10], [3 ;18 ; 11],4)
% -> 0 1.00 2.00 3.00
% 12.00 14.00 16.00 18.00
% 10.00 10.33 10.67 11.00

For two ND arrays of size P-by-Q-by-R-by-.. matrices, NDLINSPACE
(M1,M2,N) returns a P-by-Q-by-R-by-..-by-N matrix, holding N equally
space points between M1(i,j,k,..) and M2(i,j,k,..). Example:

M1 = reshape(1:2*3*4*5,[2 3 4 5]) ;
M2 = 10 * M1 ;
size(M1) ; % ans = 2 3 4 5
R = ndlinspace(M1,M2,5) ;
squeeze(R(2,1,4,3,:)).'
% ans = 68 221 374 527 680
% Compare with LINSPACE for scalars
linspace(M1(2,1,4,3),M2(2,1,4,3),5)
% ans = 68 221 374 527 680

In general, M1 and M2 have to have the same size. Scalar expansion is
applied when one of the inputs is a scalar. Example:
ndlinspace(0,[4 ; 8 ; -12],5) % expand M1
% ans = 0 1 2 3 4
% 0 2 4 6 8
% 0 -3 -6 -9 -12

See also linspace, logspace, repmat, interp1, squeeze

Cite As

Jos (10584) (2021). NDLINSPACE (v1.1, feb 2008) (https://www.mathworks.com/matlabcentral/fileexchange/22875-ndlinspace-v1-1-feb-2008), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

Steeve AMBROISE

I tried to compare this function to linspaceNDim with this code:

clear
a=rand(20,30,5,10,19,4);
b = rand(size(a));
N = 5;

tic, c1 = linspaceNDim(a, b, N);toc
clear c1

tic, c2 = ndlinspace(a, b, N);toc

I find that linspaceNDim is 20 times faster then ndlinspace.

Steeve.

MATLAB Release Compatibility
Created with R14
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!