点云学习中用到的数学知识整理

计算向量夹角

#include <iostream>
#include <Eigen/Dense>

typedef Eigen::Vector3d Point;

double getAngleTwoVectors(const Eigen::Vector3d & v1, const Eigen::Vector3d & v2) {
	double radian_angle = atan2(v1.cross(v2).norm(), v1.transpose() * v2);
	return radian_angle;   //[0,PI]
}

int main() {
	
	Point v1(2, 4, 7), v2(7, 8, 9);
	std::cout << "AngleTwoVectors: " << getAngleTwoVectors( v1, v2) << "\n";
	return 0;
}

计算每一个元素出现的次数

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; 

int main(){

	float t[5] = { 2.1, 2.2, 2.3, 2.2, 2.5 };
		vector<float> a(t,t+5);
		int j;
		for (j = 0; j<a.size(); j++) //把每个元素显示出来,便于验证结果的准确性
			cout << a[j] << "  ";      //size()的返回值类型强制转换为int,不然有警告信息,如果这样不妥当,还请高手指正
		cout << "\n\n";

		sort(a.begin(), a.end()); //sort 函数排序
		vector<float> b(j);  //创建一个和向量a 相同大小的向量b,用来计录a 中不同元素的出现次数
		int maxcount, ncount; //ncount 计录每个元素出现的次数,maxcount是出现的最高次数
		maxcount = ncount = 1;
		for (int i = 0; i < j; i++) //一共有j个元素
		{
			if ((i == j - 1) || (a[i] != a[i + 1])) //两个判断条件的前后位置不要反,j-1已经是最后一个数据了
				//所以最后一个直接输出结果,不用任何判断。
			{
				cout << a[i] << "  " << ncount << "次\n";
				b[i] = ncount; //下标i 在a中是原来的整数值,在b中是这个值出现的次数
				if (ncount > maxcount)
					maxcount = ncount;
				ncount = 1;  //每显示一次后,要确保ncount初始为1
			}
			else
				ncount++;
		}
		cout << "\n最小值:" << a[0] << "\n最大值:" << a[j - 1]
			<< "\n元素重复的最高次数是:" << maxcount << "\n其值为:";
		for (int i = 0; i < j; i++) //最高次数的元素不只有一个,分别显示出来
			if (b[i] == maxcount)
				cout << a[i] << "  ";
		return 0;
	}
	

计算位于中间的数

#include<iostream>
#include<vector>
#include<algorithm> 
using namespace std;
class Solution
{
public:
	int majorityElement(vector<int>& nums)
	{
		int n = nums.size();
		sort(nums.begin(), nums.end());//从小到大排序
		int result;
		result = nums[n / 2];//计算位于中间的数
		return result;
	}
};

int main()
{
	int a[5] = { 1, 3,3,4, 2 };
	vector<int>b(a, a + 5);
	Solution solve;
	cout << solve.majorityElement(b) << endl;

	return 0;
}

保留n位小数

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

double round(double number, unsigned int bits) {
	stringstream ss;
	ss << fixed << setprecision(bits) << number;
	ss >> number;
	return number;
}

int main() {
	double number = 3.1415926535897932;
	cout << fixed << showpoint << setprecision(15);
	cout << "一开始number = " << number << endl;

	for (int i = 0; i < 15; ++i) {
		cout << "number保留" << i << "位小数后为: "
			<< round(number, i) << endl;
	}

	return 0;
}

众数及其重数

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	float n[14] = { 1, 2, 3, 4, 5, 6, 9, 7, 2,2.00002,2.00002,2.00002,2.00002, 2.00002 };
	vector<float>b(n,n+14);
	sort(b.begin(), b.end());//数据由低到高进行排序

	for (int i = 0; i < b.size(); i++)
	{
		cout << "数组b的值为:" << b[i] << " " << endl;
	}
	int i = 0;
	int MaxCount = 1;
	int index = 0;
	while (i < b.size() - 1)//遍历
	{
		int count = 1;
		int j;
		for (j = i; j <  b.size() - 1; j++)
		{
			if (b[j] == b[j + 1])//存在连续两个数相等,则众数+1
			{
				count++;
			}
			else
			{
				break;
			}
		}
		if (MaxCount < count)
		{
			MaxCount = count;//当前最大众数
			index = j;//当前众数标记位置
		}
		++j;
		i = j;//位置后移到下一个未出现的数字
	}
	cout << "众数:"<<b[index] << " " << "重数:"<<MaxCount << endl;
	return 0;
}

求x的y次方

c++中没有表示次方的运算符,求X的Y次方,一般调用数学函数库中的pow函数,pow(X,Y)。

#include<iostream> 
#include<cmath>
using namespace std;
int main()
{
	double a;

	a = pow(2, 3);
	cout << a << endl;

	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36686437/article/details/105603985
今日推荐