vector 相关问题

向量是一维数组:它包含可以使用整数索引访问的组件。在某些语言中,矢量的大小可以根据需要增大或缩小,以适应在创建Vector之后添加和删除项目。使用'vector-graphics'进行图形显示。

R 将包含向量的列拆分为多列

我已经研究了一段时间,但还没有找到这个问题的方便答案,所以我提出了新问题,尽管它看起来很简单。 我有一个专栏(通过使用

回答 1 投票 0

c++中使用vector和使用new、delete有什么区别?

我想问一下C++中使用vector和使用new、delete的区别。 new、delete和malloc、free都用于动态内存分配。 那么我们为什么不直接使用向量呢,

回答 2 投票 0

push_back() 函数在具有结构数据类型的向量上出错?

在c++中为顶点缓冲区布局编写头文件。 #pragma 一次 #包括 #包括 结构体vertexBufferElement { 无符号整型; 无符号整数计数;

回答 1 投票 0

尝试将指针分配给结构成员时,结构的奇怪 std::vector 行为

我只是不知道如何更好地描述这个问题。我有以下代码(我使用命名空间 std 编写来简化它): #包括 #包括 使用命名空间 std;

回答 1 投票 0

std::unordered_set 与 constexpr std::vector 用于存储不可变数据

不幸的是,即使 C++23 也没有 constexpr 集:( 我希望存储适量(可能~100)的字符串,代表系统中存在的不同配置。我只需要

回答 1 投票 0

如何在矢量数据的情况下应用FFT?

我编写了以下类来计算 3d 向量列表的自相关。 我从这个链接中获取了公式 公共静态类 AutocorrVec3 { 私有静态双 C(int t, List 我编写了以下类来计算 3d 向量列表的自相关。 我从这个链接获取了公式 public static class AutocorrVec3 { private static double C(int t, List<Vec3> vectors) { int n = vectors.Count; if (t >= n || t < 0) throw new ArgumentException("Invalid value for t. It must be between 0 and n-1."); double sum = 0; for (int i = 0; i < n - t; i++) { sum += vectors[i].Dot(vectors[i + t]); } return sum / (n - t); } public static (List<double> taus, List<double> autocorrs) GetAutoCorrelationPoints(List<Vec3> vectors, int maxLag) { var tValues = new List<double>(); var cResults = new List<double>(); double c0 = C(0, vectors); // This is the normalization factor Console.WriteLine($"Normalization factor: {c0}"); for (int lag = 0; lag <= maxLag; lag++) // Start from 0 to include the autocorrelation at lag 0 { try { double cValue = C(lag, vectors); Console.WriteLine($"Lag={lag}, Raw Autocorr={cValue}, Normalized Autocorr={cValue / c0}"); tValues.Add(lag); cResults.Add(cValue / c0); // Normalization is done here } catch (ArgumentException ex) { Console.WriteLine(ex.Message); break; } } return (tValues, cResults); } } 问题是,GetAutoCorrelationPoints()非常慢。例如,我需要转换 24 个文件,每个文件包含 10000000 个 3d 矢量。 24小时后,连一个数据文件都无法完成计算。 在这种情况下我该如何应用FFT? 我想用MathNet.Numerics。 using System; using System.Collections.Generic; using System.Numerics; using MathNet.Numerics.IntegralTransforms; namespace FourierTransformCSharp { public static class AutocorrVec3 { // Compute the autocorrelation of a time series using FFT public static double[] ComputeAutocorrelationUsingFFT(List<Vec3> vectors) { int n = vectors.Count; // Create a zero-padded list double the size of the original for FFT var paddedVectors = new Complex[2 * n]; for (int i = 0; i < n; i++) { // Convert vector to complex number with magnitude as real part paddedVectors[i] = new Complex(vectors[i].Magnitude(), 0); } for (int i = n; i < 2 * n; i++) // Zero padding { paddedVectors[i] = Complex.Zero; } // Perform FFT on the zero-padded list Fourier.Forward(paddedVectors, FourierOptions.Default); // Compute power spectrum (magnitude squared) for (int i = 0; i < paddedVectors.Length; i++) { var magnitude = paddedVectors[i].Magnitude; paddedVectors[i] = new Complex(magnitude * magnitude, 0); } // Perform Inverse FFT to obtain the autocorrelation function Fourier.Inverse(paddedVectors, FourierOptions.Default); // Extract the real parts as the autocorrelation result var autocorr = new double[n]; for (int i = 0; i < n; i++) { autocorr[i] = paddedVectors[i].Real; } // Normalize the autocorrelation result var normalizationFactor = autocorr[0]; for (int i = 0; i < n; i++) { autocorr[i] /= normalizationFactor; } return autocorr; } // Calculate autocorrelation of vector time series at lag t public static double C(int t, List<Vec3> vectors) { double[] autocorr = ComputeAutocorrelationUsingFFT(vectors); return autocorr[t]; } // Get autocorrelation values for lags from 0 to maxLag public static (List<int> taus, List<double> autocorrs) GetAutoCorrelationPoints(List<Vec3> vectors, int maxLag) { List<int> taus = new List<int>(); List<double> autocorrs = new List<double>(); double[] autocorr = ComputeAutocorrelationUsingFFT(vectors); for (int t = 0; t <= maxLag && t < vectors.Count; t++) { taus.Add(t); autocorrs.Add(autocorr[t]); } return (taus, autocorrs); } // Parallel computation is unnecessary as FFT-based autocorrelation is already fast // Use GetAutoCorrelationPoints for parallel computation } public class Vec3 { public double X { get; } public double Y { get; } public double Z { get; } public Vec3(double x, double y, double z) { X = x; Y = y; Z = z; } // Compute the magnitude of the vector public double Magnitude() { return Math.Sqrt(X * X + Y * Y + Z * Z); } } public class AutocorrelationDriver { public static void Main() { // Define your list of 3D vectors List<Vec3> vectors = new List<Vec3> { new Vec3(1, 1, 1), new Vec3(2, 2, 2), new Vec3(3, 3, 3), new Vec3(4, 4, 4), new Vec3(5, 5, 5) }; // Compute the autocorrelation var aurocorr = AutocorrVec3.GetAutoCorrelationPoints(vectors, vectors.Count - 1); // Output the results Console.WriteLine("Lag\tAutocorrelation"); for (int i = 0; i < aurocorr.taus.Count; i++) { Console.WriteLine($"{aurocorr.taus[i]}\t{aurocorr.autocorrs[i]}"); } Console.ReadKey(); } } } 上面的代码是我写的。 正确的输出如下: 但是,我编写的代码给出了以下输出: Lag Autocorrelation 0 1 1 0.727272727272727 2 0.472727272727273 3 0.254545454545455 4 0.0909090909090911 如何修复我的代码? 就目前而言,基于 FFT 的计算实际上没有任何问题。您记得在所有正确的位置进行零填充(做得很好),它给出了我对您的测试数据所期望的答案。 这两个代码之间的差异是因为您忘记了通过贡献滞后分量 1/(n-t) 的数量来标准化 FFT 计算的输出。 应用该校正进行 FFT 相关性计算可得到与玩具测试数据上的普通方法完全相同的答案。 我不太确定将相关函数应用于矢量大小的优点,但这完全是另一回事。这是示例数据的表格。 数据 滞后 sum(a(i).a(i+t)) 规范 /(n-t) 范数 C(t,v5) 1 0 55 1 0.2 1 2 1 40 0.727272727 0.181818182 0.909090909 3 2 26 0.472727273 0.157575758 0.787878788 4 3 14 0.254545455 0.127272727 0.636363636 5 4 5 0.090909091 0.090909091 0.454545455 您可以通过一种方法来进一步优化它,方法是返回向量中的幂,而不是返回magnitude = sqrt(X.X+Y.Y+Z.Z),然后在返回“X.X+Y.Y+”时对其进行平方Z.Z" 它已准备好用于计算功率谱。我真的不确定这个计算有什么物理解释。 顺便说一句,通过使用 FFT 的实数到复数共轭对称版本,您几乎可以将速度提高一倍并将内存需求减半。这避免了将真实数据提升为零虚部的复杂数据。首先尝试“按原样”,因为对于较大的 N,Nlog N 比 N^2 小很多。 但是速度提高两倍可能仍然值得付出额外的努力。

回答 1 投票 0

从同一向量中推回一个元素是否安全?

向量v; v.push_back(1); v.push_back(v[0]); 如果第二次push_back导致重新分配,则对向量中第一个整数的引用将不再有效。那么这不安全吗?

回答 10 投票 0

有没有办法防止 std::vector 在发生调整大小时动态取消分配内存?

我想使用 std::vector 代替原始 C 数组,以避免弄乱我的代码。 然而,std::vector 的活力的挫折之一是底层数据不可靠......

回答 1 投票 0

在 C++ 中使用 2D 向量时出现分段错误 (SIGSEGV)

我正在 GFG 上解决这个问题。我写的解决方案是 向量 > stockBuySell(向量 A, int n){ 矢量>天; int p=0...

回答 1 投票 0

将坐标 (0,0) 替换为 (10^-5 , 10^-5)

我有一个关于Matlab处理坐标(0,0)替换的问题。我知道如果我将值(0,0)分别插入fx和fy中,就会出现错误。那么什么是合成物...

回答 1 投票 0

使用python计算3d空间中反射向量的新方向

我有一个单元格,在我的入射光束击中单元格后,该单元格具有法线矢量,我计算法线光束和入射光束之间的角度,角度为 45 度,现在我想计算新的方向...

回答 1 投票 0

Getline 只返回空白

我正在尝试连续打开多个文件。文件名全部存储在一个向量中,该向量被传递给我的函数。这只是一个简单的测试,以确保一切正常。如果有效的话...

回答 2 投票 0

为什么我的 C++ 程序在使用调试器时运行良好,但在没有调试器时却无法运行?

我正在尝试重新创建向量类及其基本功能,但我的程序在操作过程中不断退出。当我运行调试器时,它完全执行得很好。这是我的...

回答 2 投票 0

将文档上传到 Azure AI 搜索

我正在使用 azure AI 搜索实现 RAG。我已经创建了索引,总共有 2605 个文档块要上传到索引。我观察到的奇怪行为是: 我无法上传全部

回答 1 投票 0

python-oracledb 在使用oracle的向量类型时出错

我正在尝试在版本23.4上获取oracle的新向量类型,我正在使用python-oracledb,但我收到一条错误消息“没有DB_TYPE_VECTOR的属性”,我是否运行了错误的版本...

回答 1 投票 0

设置玩家速度,用 bukkit 将其从 A 点发射到 B 点

我正在尝试为服务器编写一个插件,我想从 A 点到 B 点启动一个播放器。 我已经尝试过这个,但很多时候它不准确并且错过了几个街区。 私人

回答 1 投票 0

错误:流体模拟属性梯度计算中没有运算符“+=”与 float 和 Vector2 匹配

我正在尝试使用 C++ 和 OpenGL 来观看 Sebastian Lague 的流体模拟视频。我现在正在计算 propertyGradient 值(时间戳是 14:15)。 我会尽力提供所有代码...

回答 1 投票 0

Matlab:数值向量的定积分

我正在寻找求解两个向量的定积分,我尝试解释一下。 我知道:数字的 z 向量和数字的 G(z) 向量。 积分的极限是 zmin 和 zmax 我应该打电话...

回答 1 投票 0

使用 MATLAB 对函数 f(x) 对 x 进行数值积分,其中 f(x) 有另一个参数 y,它是一个向量

我想对一个向量进行数值积分,该向量表示在Matlab中由边界x0和x1指定的x范围内的函数f(x)。我想检查集成的输出...

回答 2 投票 0

关于向量(玩家速度)和Spigot API的问题

我不明白如何将玩家向后、向左和向右推,我只知道如何将玩家推到他正在看的一侧。 我尝试使用 ChatGPT 但它生成的代码不...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.