我正在尝试将一个void**指针转换为C中的int**2D数组
这是我正在尝试使用的代码(删除了所有无关的位):
\*assume that i have a data structure called graph with some
*element "void** graph" in it and some element "int order" */
void initialise_graph_data(graph_t *graph)
{
void **graph_data = NULL;
int (*matrix)[graph->order];
size_t size = (graph->order * graph->order) * sizeof(int);
graph_data = safe_malloc(size); /*safe malloc works fine*/
matrix = (int(*)[graph->order])graph_data;
graph->graph = graph_data;
}
Run Code Online (Sandbox Code Playgroud)
当我编译它,它工作正常,但给我一个警告,变量'矩阵'设置但不使用.我真的不想使用临时矩阵变量,因为该函数只是初始化数组,而不是放入任何内容; 但是,如果我尝试将graph_data直接转换为int**,当我将其分配给graph-> graph时,如下所示:
graph->graph = (int(*)[graph->order])graph_data;
Run Code Online (Sandbox Code Playgroud)
它给我一个不兼容的指针类型警告的分配.
我不是正确地施展它?有没有人有任何建议,如何没有临时"矩阵"变量使其工作?或者如果没有,我可以对该变量做什么,以便它不会给我警告它已设置但未使用?
谢谢
有什么方法可以使用 matplotlib 指定文本框的右下角坐标吗?
我的盒子的代码是:
plt.text(x_max-2.7e+3, y_min+1e+5, box_text, style='italic',
bbox={'facecolor':'white', 'alpha':0.5, 'pad':10})
Run Code Online (Sandbox Code Playgroud)
并为当前的情节产生这个:
然而,这些坐标值是通过反复试验找到的,对于不同的图,我的数据的范围和数量级也会有所不同。我想要一种可以指定右下角坐标的方法文本框为 (x_max, y_min) ,它会自动从那里绘制框。
有没有办法做到这一点?
我正在尝试使用 Processing 为基本的 Kinect (v2) 深度云调整 Daniel Shiffman 的代码,但是屏幕中间总是有一个像素不会去任何地方,这很烦人。这是我的意思的一个例子。
您可以看到它似乎就在前面,并且在视野中的任何物体移动时都不会移动。
这是我用来生成上面图像的代码,(这是我正在尝试做的非常精简的版本)
// imports for openkinect
import org.openkinect.freenect2.*;
import org.openkinect.processing.*;
import java.nio.FloatBuffer;
// dots size is dot_size*skip
int dot_size = 2;
// step size when iterating through pixel array
int skip = 5;
// Kinect Library object
Kinect2 kinect2;
// Angle for rotation
float a = 3.1;
void setup() {
// Rendering in P3D
size(1500,1000,P3D);
// start the kinect
kinect2 = new Kinect2(this);
kinect2.initDepth();
kinect2.initDevice();
smooth(16);
// Black background
background(0);
} …Run Code Online (Sandbox Code Playgroud) 我来自matlab背景到python,我只是想知道python中是否有一个简单的运算符将执行以下功能:
a = [1, 0, 0, 1, 0, 0]
b = [0, 1, 0, 1, 0, 1]
c = a|b
print c
[1, 1, 0, 1, 0, 1]
Run Code Online (Sandbox Code Playgroud)
或者我必须写一个单独的功能来做到这一点?
我有一个由邻接矩阵表示的图,G我尝试使用 DFS 删除导致循环的边
可以有多个循环,但我认为最好一次删除它们,所以我只需要我的算法找到一个循环,并且可以重复。
这是我到目前为止所得到的代码:
function [ G, c_flag, c_stack, o_stack, cptr, optr ] =...
dfs_cycle( G, curr_v, c_stack, o_stack, cptr, optr, c_flag )
% add current vertex to open list
optr = optr + 1;
o_stack(optr) = curr_v;
% find adjacent vertices
adj_v = find(G(curr_v,:));
for next_v = adj_v
% ensure next_v is not in closed list
if ~any(c_stack == next_v)
% if next_v in open list then cycle exists
if any(o_stack == next_v)
% remove edge …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 RGB 三元组创建一个非常简单的颜色图。它具有离散数量的颜色。
这是我试图让它遵循的形状:
我知道它可能不是真正的 RGB 光谱,但我希望它尽可能容易计算。
我希望能够生成沿线均匀分布的 N 个 8 位 RGB 元组的数组。
对于上图中所有点都落在颜色上的简单实例,可以numpy.linspace()使用以下代码轻松生成:
import numpy as np
# number of discrete colours
N = 9
# generate R array
R = np.zeros(N).astype(np.uint8)
R[:int(N/4)] = 255
R[int(N/4):int(2*N/4)+1] = np.linspace(255,0,num=(N/4)+1,endpoint=True)
# generate G array
G = 255*np.ones(N).astype(np.uint8)
G[0:int(N/4)+1] = np.linspace(0,255,num=(N/4)+1,endpoint=True)
G[int(3*N/4):] = np.linspace(255,0,num=(N/4)+1,endpoint=True)
# generate B array
B = np.zeros(N).astype(np.uint8)
B[int(2*N/4):int(3*N/4)+1] = np.linspace(0,255,num=(N/4)+1,endpoint=True)
B[int(3*N/4)+1:] = 255
# stack arrays
RGB = np.dstack((R,G,B))[0]
Run Code Online (Sandbox Code Playgroud)
此代码适用于 5 种颜色:
r 255 255 …Run Code Online (Sandbox Code Playgroud) 如果我想找到两个无序集合的并集,表示为1D向量,例如:
a = [2 4 6 8 1]
b = [1 2 5 7 9]
Run Code Online (Sandbox Code Playgroud)
我可以使用union函数:
c = union(a,b)
Run Code Online (Sandbox Code Playgroud)
给出了答案:
c = [1 2 4 5 6 7 8 9]
Run Code Online (Sandbox Code Playgroud)
然而,这似乎相当缓慢(相对而言).如果我对它进行tic-toc测试,我得到:
>> for test = 1
tic
c = union(a,b);
toc
end
Elapsed time is 0.000906 seconds.
Run Code Online (Sandbox Code Playgroud)
然而,如果我使用这种更复杂的方法,我得到一个更快的结果:
>> for test = 1
tic
a_1 = zeros(1,9);
b_1 = zeros(1,9);
a_1(a) = 1;
b_1(b) = 1;
c_1 = or(a_1,b_1);
c = find(c_1);
toc
end
Elapsed time is 0.000100 seconds.
Run Code Online (Sandbox Code Playgroud)
对于c,这仍然给出了相同的答案,但是快了大约9倍(用这个小例子,我不确定它的缩放程度) …
我有一个类,它有一些存储在向量中的数据和一个应该选择随机元素并返回它的方法,但是当我运行它时,它每次都返回相同的元素.
这是一个基于我的代码的简化示例:
#include <iostream>
#include <random>
#include <vector>
class MyObj{
private:
std::vector<int> set_data;
public:
MyObj(int num_elements){
for (int i = 0; i < num_elements; ++i){
set_data.push_back(i); // just so that there is some data in there
}
};
int getRandomElement(std::mt19937 rng){
std::uniform_int_distribution<int> uni(0,set_data.size()-1);
int idx = uni(rng);
return set_data[idx];
};
};
int main()
{
std::random_device r;
std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
std::mt19937 rng = std::mt19937(seed);
MyObj temp(50);
for (int i = 0; i < 20; i++){ …Run Code Online (Sandbox Code Playgroud) 最“Pythonic”的方法是什么?
x = some_value
bounds = [lower_bound, upper_bound]
if x < bounds[0]:
x = bounds[0]
if x > bounds[1]:
x = bounds[1]
Run Code Online (Sandbox Code Playgroud) python ×4
matlab ×2
algorithm ×1
c ×1
c++ ×1
casting ×1
colormap ×1
graph-theory ×1
kinect ×1
matplotlib ×1
numpy ×1
openkinect ×1
operators ×1
performance ×1
processing ×1
random ×1
range ×1
set ×1