File Exchange

image thumbnail

Linearly spaced multidimensional matrix without loop

version (2.52 KB) by Steeve AMBROISE
Generates a N-dimensional matrix of linearly equally spaced points between the matrices d1 and d2.


Updated 19 May 2014

View Version History

View License

LINSPACENDIM is a good extension of the Matlab function linspace to any multidimensional elements.
LINSPACENDIM(d1, d2) generates a multi-dimensional
matrix of 100 linearly equally spaced points between
each element of matrices d1 and d2.

LINSPACENDIM(d1, d2, N) generates N points between
each element of matrices X1 and X2.

d1 = rand(3, 2, 4); d2 = rand(size(d1)); n = 10;

y = linspaceNDim(d1, d2, n) returns a multidimensional matrix y of
size (3, 2, 4, 10)

Class support for inputs X1,X2:
float: Multidimensional matrix, vector, double, single

--> I did not use any Loop! <--

I use an old function that I wrote to make the tensor product of two(2) multidimensional matrices of of any size and any number of dimensions.

Cite As

Steeve AMBROISE (2021). Linearly spaced multidimensional matrix without loop (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (14)

John McDowell

Humam Helfawi

I just wondering why there is no consistency in the output dimension. If the input is single value the result will be N*1 vector. However, if the input is vector of values, the output will be k*N.
Thanks again for the great work!

Mohammed Salman

Alessandro Masullo

In 2D you can use these simple commands, that are much faster:

A = randi(10,1000,1);
B = randi(10,1000,1)+20;
N = 1500;

dx = (B-A)/(N-1);
AB = repmat(dx,1,N);
AB(:,1) = A;
AB = cumsum(AB,2);


I've found this code very helpful. However, there is a minor issue. The output in the case of scalar arguments d1 and d2 is a column vector, while in the case of arguments of higher dimensionality, the output is transposed (it is a matrix consisting of raw vectors, corresponding to each element of the argument). If I'm not clear, just take a look how the output looks like when d1 and d2 are scalars, and when they are vectors 1 by 2.

I corrected the code by adding in the end of the function the following line:

if NDim == 0, y = y'; end

It changes the output of the scalar arguments case a raw vector, consistent with argument of higher dimensionality and with the original linspace function.



Steeve A.

You're welcome Pio.

Pio Nonus

You saved me a lot of time

Jos (10584)


Great program!

Jos, you have to change your rating.


Jos (10584)

The error indeed seems to be fixed.

Steeve A.

I tried it after modifications. It works perfectly.


Jos (10584)

>> linspacendim(0,1)
??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> LINSPACENDIM at 39
y = cat(NDim+1, Sum1 + Sum2, d2);

This error occurs for every 1-by-N-by-M-... matrix. There are some other issues as well. For instance, in the help:
- n = 1 in the example?

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

Inspired: NDLINSPACE (v1.1, feb 2008)

Community Treasure Hunt

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

Start Hunting!