The fastest way I can think of is calculating the step-size, construct the vector from that using implicit binary expansion.

 a = [ -1; 0; 1];
 n = 3;
 stepsizes = (max(a,0)-min(a,0))/(n-1);
 A = min(a,0) + (0:(n-1)).*stepsizes;

Timeit:

A couple of timeit results using (use timeit(@SO) and remove comments from the blocks to be timed):

function SO()
n = 1e3;
m = 1e5;
a = randi(9,m,1)-4;

% %Wolfie
% aminmax = [min(a, 0), max(a,0)]';
% A = interp1( [0,1], aminmax, linspace(0,1,n) )';

% %Nicky
% stepsizes = (max(a,0)-min(a,0))/(n-1); 
% A = min(a,0) + (0:(n-1)).*stepsizes;

% %Loop
% A = zeros(m,n);
% for i=1:m
%     A(i,:) = linspace(min(a(i),0),max(a(i),0),n);
% end

%Arrayfun:
A = cell2mat(arrayfun(@(x) linspace(min(x,0),max(x,0),n),a,'UniformOutput',false));

Then the times are:

  • Wolfie: 2.2243 s
  • Mine: 0.3643 s
  • Standard loop: 1.0953 s
  • arrayfun: 2.6298 s
Answer from Nicky Mattsson on Stack Overflow
🌐
MathWorks
mathworks.com › matlabcentral › answers › 165615-create-equally-spaced-2-d-array
Create equally spaced 2-d array - MATLAB Answers - MATLAB Central
December 5, 2014 - https://www.mathworks.com/matlabcentral/answers/165615-create-equally-spaced-2-d-array#answer_161450 ... Your code is correct, you've just mistaken in the line [arrayName(k,:) = linspace(0,k*2*pi, 100*k )], you should write 100*n instead of 100*k
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 22824-linearly-spaced-multidimensional-matrix-without-loop
Linearly spaced multidimensional matrix without loop - File Exchange - MATLAB Central
May 19, 2014 - 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 use an old function that I wrote to make the tensor product of ...
🌐
MathWorks
mathworks.com › matlabcentral › answers › 1750320-creating-a-2d-grid-with-evenly-spaced-points
Creating a 2D grid with evenly spaced points - MATLAB Answers - MATLAB Central
June 29, 2022 - https://www.mathworks.com/matlabcentral/answers/1750320-creating-a-2d-grid-with-evenly-spaced-points#answer_996265 · Cancel Copy to Clipboard · Ran in: Read about meshgrid · x = linspace(0,1,10) ; y = linspace(0,1,10) ; [X,Y] = meshgrid(x,y) ; plot(X,Y,'.r') aman verma on 29 Jun 2022 ·
🌐
MathWorks
mathworks.com › matlab › graphics › 2-d and 3-d plots › line plots
plot - 2-D line plot - MATLAB
Use the linspace function to define x as a vector of 150 values between 0 and 10.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 378693-how-to-create-a-non-uniform-2d-grid
How to create a non-uniform 2d grid? - MATLAB Answers - MATLAB Central
January 24, 2018 - https://www.mathworks.com/matlabcentral/answers/378693-how-to-create-a-non-uniform-2d-grid · Cancel Copy to Clipboard · Commented: Abdalrhaman Koko on 5 Dec 2020 · Open in MATLAB Online · Hi all, I have created a grid as shown in below image · But my aim is to create a grid in the following way · Can anyone help me in creating this type of mesh. Please find below code, clear all · clc · L=1; nx=50; ny=50; x1=linspace(0,0.4,10); x2=linspace(0.42,1,40); x=cat(2,x1,x2); y1=linspace(0,0.4,10); y2=linspace(0.42,1,40); y=cat(2,y1,y2); [X,Y]=meshgrid(x,y); plot(X,Y,'k',Y,X,'k'); Thank you all.
Find elsewhere
Top answer
1 of 1
3

You can create a linear spline with the points in a as control points. After that you can specify as many points as you want from a beginning interval to an ending interval. As what Raab70 said, you can use interp1. interp1 can be called in the following way (using linear interpolation):

out = interp1(x, y, xp, 'linear')

x are the x values and y are the y values for the control points. xp are the points you want to evaluate the function at. Because you don't explicitly have x values and you just want the y values, you can create a dummy vector that goes from 1 up to L where L is how many points are in your output data. This dummy vector is for your x values. After, specify y as your output data. In this case, it is the array of a. Next, you need to specify where you want to sample along the curve. Because you want to introduce 5 points in between each space, you will have in total 5*4 + 5 = 25 points all together. 5 points per "slot", and there are 4 slots all together. You also include the 5 points that were from your original output data. To create these 25 points, simply perform linspace from 1 up to 5 and specify that you want 25 points in between this interval. This should perfectly capture the control points (the dummy values of 1,2,3,4,5) as well as the values that you want to use to interpolate in between each of the control points.

As such, try something like this:

N = 5; %// Number of points to introduce in between each control point
y = [0 0 1 0 0]; %// Your output data
L = numel(y); %// Size of output data. Cache so we don't have to keep typing in numel(y)
x = 1:L; %// Dummy vector
xp = linspace(1, L, N*(L-1) + N); %// Create points to interpolate. N*(L-1) + N is also just N*L
out = interp1(x, y, xp, 'linear'); %// Generate interpolated array

out thus gives me:

out =

Columns 1 through 9

     0         0         0         0         0         0         0    0.1667    0.3333

Columns 10 through 18

0.5000    0.6667    0.8333    1.0000    0.8333    0.6667    0.5000    0.3333    0.1667

Columns 19 through 25

     0         0         0         0         0         0         0
🌐
GeeksforGeeks
geeksforgeeks.org › linearly-spaced-vector-in-matlab
Linearly Spaced Vector in MATLAB - GeeksforGeeks
November 28, 2022 - More clearly, say one wants to ... MATLAB provides a very simple function to create this linearly spaced vector, the linspace() function....
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 32512-n-dimensionally-spaced-points
N-dimensionally spaced points - File Exchange - MATLAB Central
August 14, 2011 - x = ndspace([0,0],[10,100],5,@linspace) y = ndspace([0,1],[1,2],6,@logspace) z = ndspace([10,1],[20,2],[5,8],{@linspace,@logspace}) plot(x(:,1),x(:,2),'bo'), hold on plot(y(:,1),y(:,2),'gx') plot(z(:,1),z(:,2),'r*'), hold off · Christophe Lauwerys (2026). N-dimensionally spaced points (https://www.mathworks.com/matlabcentral/fileexchange/32512-n-dimensionally-spaced-points), MATLAB Central File Exchange.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 110674-help-me-about-plot-and-linspace-in-matlab
Help me about plot and linspace in matlab ? - MATLAB Answers - MATLAB Central
December 25, 2013 - for example I plot the graph *y = 2/x* by plot command : x = linspace(1,10,100); y = 2./x; plot(x,y,'linewidth',4,'color','r') It run normally and doesn't have error. But now I plot the ...
🌐
GeeksforGeeks
geeksforgeeks.org › software engineering › 2d-array-interpolation-in-matlab
2D Array Interpolation in MATLAB - GeeksforGeeks
December 6, 2022 - % MATLAB code for linspace() and interp2() % Initializing a 2d array of dimensions 4*4 A=[0.69 1.76 0.089 2.0012; 5.89 1.25 0.47 0.55; 1.06 1.25 1.134 4.163; 2 1.7 0 2.4]; % Calling the linspace() function to % generate the row vectors of 7 points % in between and including 1 to 3 x = linspace(1, 3, 7); % Calling the interp2() function for % interpolation of above specified 2d array result = interp2(A, x(:), x)
🌐
MathWorks
mathworks.com › matlab › mathematics › interpolation
ndgrid - Rectangular grid in N-D space - MATLAB
Specifically, the first two dimensions of a grid created using one of these functions are swapped when compared to the other grid format. Some MATLAB® functions use grids in meshgrid format, while others use ndgrid format, so it is common to convert grids between the two formats.
🌐
MathWorks
mathworks.com › videos › linearly-spaced-vectors-97476.html
Linearly Spaced Vectors - MATLAB
When I look at code written by new users, sometimes, I will see code like this: a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]; There is an easier way: a = 1:13; This three minute video shows how to use commands like linspace and the colon operator to mak
Published   October 26, 2007
🌐
Ansys
optics.ansys.com › hc › en-us › articles › 360034404014-Creating-2D-image-plots-with-MATLAB
Creating 2D image plots with MATLAB – Ansys Optics
x = linspace(-1.5,0.5,100); y = [linspace(-2,0,20); linspace(0.01,1,100)]; X = meshgridx(x,y); Y = meshgridy(x,y); A = exp(-(1+3*X)^2-(0.5+Y)^2); matlabsave("toPlot.mat",x,y,A); image(x,y,A); load('toPlot.mat'); % create linearly spaced vectors x2 = linspace(min(x),max(x),200); y2 = linspace(min(y),max(y),200); % create 2D mesh grids.