Skip to content

Instantly share code, notes, and snippets.

@damithsj
Created August 27, 2014 12:07
Show Gist options
  • Save damithsj/c96a8482b282a3dc89bd to your computer and use it in GitHub Desktop.
Save damithsj/c96a8482b282a3dc89bd to your computer and use it in GitHub Desktop.
C++ implementation of linspace function in MatLab. This function generates N points between min and max and return as vector.
#include <cmath>
#include <vector>
using namespace std;
//linspace function in MatLab. hacked the code ;)
// ...\toolbox\matlab\elmat\linspace.m
//generates N points between min and max and return as vector.
vector<double> linspace(double min, double max, int n)
{
vector<double> result;
// vector iterator
int iterator = 0;
for (int i = 0; i <= n-2; i++)
{
double temp = min + i*(max-min)/(floor((double)n) - 1);
result.insert(result.begin() + iterator, temp);
iterator += 1;
}
//iterator += 1;
result.insert(result.begin() + iterator, max);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment