- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个高效的代码来执行循环移位,在我的数据处理过程中,我需要在大矩阵上多次实现它。
在我的第一次试验中,编译器抛出一些异常,似乎我可能正在尝试访问超出其大小的矩阵元素,但我不知道发生了什么问题。
1) 我也在使用 Armadillo lib
,它有 "mat"
定义。2) 我打算按行和/或列移动它。
这是我的尝试:
#include "stdafx.h"
#include <vector>
#include <iostream>
#include "C:\Users\kumar\Documents\Visual Studio 2012\UserLibs\armadillo-3-910-0\include\armadillo"
#include <stdlib.h> /* srand, rand */
using namespace arma;
template<class ty>
void circshift(ty *out, const ty *in, int xdim, int ydim, int xshift, int yshift)
{
int iOutputInd, iInputInd, ii, jj;
for (int i =0; i < xdim; i++)
{
ii = (i + xshift) % xdim;
for (int j = 0; j < ydim; j++)
{
jj = (j + yshift) % ydim;
iOutputInd = ii * ydim + jj;
iInputInd = i * ydim + j;
std::cout << " iOutputInd --> " << iOutputInd << " ; iInputInd -->" << iInputInd << "\n";
out[iOutputInd] = in[iInputInd]; // EXCEPTION BEING THROWN HERE
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//a = [1 2 3; 4 5 6; 7 8 9];
mat a, a_out; // "mat" defined in C++ lib Armadillo
a << 1 << 2 << 3 << endr
<< 4 << 5 << 6 << endr
<< 7 << 8 << 9 <<endr;
a.reshape(3,3);
//a.print();
a_out = a;
int xdim = 3; int ydim = 3; int xshift = 1; int yshift = 0;
circshift(&a_out, &a, xdim, ydim, xshift, yshift);
a_out.print();
return 0;
}
它编译得很好。但是,当我尝试运行时,Visual Studio 抛出以下错误:
Unhandled exception at 0x3FF00000 in Circshift_Example.exe: 0xC0000005: Access violation (parameters: 0x00000008).
我在 visual studio 控制台中收到另一个错误,它提示:
error: Mat::init(): requested size is too large
更新:最终解决方案我发布了我的代码,因为它可能对某些用户有用。
请注意,我正在使用“Armadillo”库来创建矩阵。可以用自己的矩阵类替换 Armadillo “垫”类。
如果您使用此代码,请投票。
#include "stdafx.h"
#include "armadillo-3-910-0\include\armadillo"
using namespace arma;
template<class ty>
void circshift(ty& out, const ty& in, int xshift, int yshift)
{
int iOutputInd, iInputInd, ii, jj;
int ydim = in.n_cols;
int xdim = in.n_rows;
for (int j =0; j < ydim; j++)
{
jj = (j + yshift) % ydim;
if (jj <0) jj = jj + ydim;
for (int i = 0; i < xdim; i++)
{
ii = (i + xshift) % xdim;
if (ii <0) ii = ii + xdim;
out[jj * xdim + ii] = in[j * xdim + i];
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//a = [1 2 3; 4 5 6; 7 8 9];
mat a, a_out;
a << 1 << 2 << 3 << endr
<< 4 << 5 << 6 << endr
<< 7 << 8 << 9 <<endr;
a.reshape(3,3);
a_out = a;
int xshift = 1; int yshift = 0;
circshift(a_out, a, xshift, yshift);
a_out.print();
xshift = 1; yshift = -1;
circshift(a_out, a, xshift, yshift);
a_out.print();
return 0;
}
最佳答案
这里的主要错误是您将指向 mat
类型对象的指针传递给 circshift()
函数(out
和 in
参数,然后将这些参数用作 mat
的数组。以下行未按照您的想法进行解释
out[iOutputInd] = in[iInputInd];
因为 out
和 in
不是 mat
对象。它们是指向 mat
对象的指针,因此编译器会将 in
和 out
解释为指向 mat
数组的指针并索引这些数组,将不存在的 mat
从 in[...] 复制到另一个不存在的位置。
解决这个问题的一个简单方法是使用引用而不是指针来传递 mat
对象,即:
template<class ty> void circshift(ty& out, const ty& in, int xdim, int ydim, int xshift, int yshift)
{
...
}
并在 _tmain 中调用它:
circshift(a_out, a, xdim, ydim, xshift, yshift);
关于c++ - C/C++ : circular shift on matrices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28305054/
环形缓冲区和循环链表有什么区别? Ring Buffer 的目的是什么,而循环链表不能,反之亦然? 最佳答案 环形缓冲区是一个连续的内存块,其中包含您的项目,当您到达终点时,您会循环回到起点: +--
我有一个像这样的多级依赖关系图,我需要检测该图中的任何循环引用。 A = B 乙=丙 C = [D, B] D = [C, A] 有人有这样的问题吗? 有什么解决办法??? 用英语表示感谢和抱歉。 =
尝试编写一个reduce函数来过滤掉任何重复项。我知道还有其他方法可以解决这个问题,但我正在尝试练习递归函数。 function addToSet(a, b) { a.add(b); retu
我想知道是否有人已经实现/知道(最好是 javascript)将处理循环间隔的间隔树算法。通过循环,我的意思是开始 > 结束的间隔。请注意,这也需要限制间隔的大小。 这只是常见区间树问题的一个子案例吗
我有一段代码,其中声明了两种数据类型。我已经将两者都自动派生为类型类 enum 的成员,但是,我不喜欢它们不是“圆形”的。我的意思是调用 succ Sun应该让我Mon . succ Dec应该导致
运行此代码时: function arrayToList(arr) { if (arr.length == 1) { return { value: arr[0],
我有以下模块结构: /components ├── Button.js ├── Checkbox.js ├── index.js ├── DateSelect ├── DateSelect.j
为了检查光线与三角形的碰撞,我们可以首先查看光线是否与三角形的平面发生碰撞。如果是,我们然后检查所有三角形边的交点是否在同一边。如果为真,则意味着该点位于三角形内部。此过程与矩形和其他凸图形类似。 这
我遇到了同样的问题,如下所示: NHibernate - not-null property reference a null or transient value 根本原因就像 Alun Harfo
我想以“循环”方式使用函数,如下例所示: fun cll1 (s)= cll2(s); fun cll2 (s)= cll3(s); fun cll3 (s)= cll(s); 编写此代码会在 SML
我想根据现有列的滞后值在 pyspark.sql.DataFrame 中创建一个新列。但是......我也希望最后一个值成为第一个值,第一个值成为最后一个值。这是一个例子: df = spark.cr
我正在使用 Microsoft.ServiceModel.Samples.CircularTracecListener 二进制文件来实现 Web 服务上的错误日志记录。 它应该写入一个文件,直到达到
我有一个由 8 个罗盘点组成的数组,从 SW 顺时针编号到 S: 2 3 4 1 5 0 7 6 我想计算从一点到另一点的最短路线是顺时针 (+1) 还是逆时针 (-1)。例如。从 7 到 5 是
我知道如何解决基本的循环依赖示例,有两个类,每个类都需要知道另一个存在。 但是,我现在的情况是,示例更复杂,前向声明不是可以解决该问题的东西。 考虑这三个文件 // my_thread.hpp tem
我正在开发一款用于在线播放歌曲的应用程序。我可以通过点击播放这首歌。 我的问题是我在所有 Activity 中都有一个 CircularSeekBar,我需要在所有屏幕上管理它。如果从任何 Activ
我正在尝试实现动画 shown here .它工作正常,除了 unreveal 动画,显示黑屏片刻。并非所有设备/api 版本都存在该故障。 Google Nexus 10 (API 22) 的 Ge
我正在使用 Canvas 创建倒数计时器循环,但有一个简单的问题,我需要将天数除以 364 天,当天数大于 364 天时,天数消失: 我需要的是这样的: (function($) { jQuery
我想在不使用 SVG 的情况下实现这样的效果: https://codepen.io/arforcee/pen/erbKi?limit=all&page=2&q=circular+loader 这是否
这个问题在这里已经有了答案: How to create a circle with links on border side (8 个答案) 关闭 4 年前。
我遇到了一个小设计问题。 如下情况 库界面 包含每个模型类的接口(interface)(仅限 getter 和 setter) 图书馆业务逻辑 包含接口(interface)库和 DAL 的实现。使用
我是一名优秀的程序员,十分优秀!