Created
August 27, 2014 12:07
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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