标签: matrix

C矩阵的记忆是连续的吗?

我正在尝试在C中执行一个以矩阵作为参数的函数.
我只会在运行时知道矩阵有多少元素(但是是NxN矩阵).

因此,我想知道是否有保证空间总是连续的,因此我只能询问指向第一个元素的指针.我宣布我的矩阵是这样的:

int nodo;
scanf("%d", &nodo);
int distancias[nodo][nodo];
Run Code Online (Sandbox Code Playgroud)

c matrix

0
推荐指数
1
解决办法
207
查看次数

通过给定行向量及其对应的位置矩阵将值替换为矩阵


我有一个A = [m,n]矩阵和一个B = [1,n]矩阵.

A是对于m行具有1:n的排列的矩阵.

A =
[2 4 1 5 3
5 2 3 4 1
1 2 3 4 5]

B只是一个普通的行向量(每个单元格的值不是必需的唯一)

B = [0.05 0.03 0.06 0.04 0.02]

现在,我想根据Matrix A的每一行置换B.例如,

i = 1
B(指数(i,:))= [0.03 0.04 0.05 0.02 0.06]

在for循环之后,那是我想要的结果:

C =
[0.03 0.04 0.05 0.02 0.06
0.02 0.03 0.06 0.04 0.05
0.05 0.03 0.06 0.04 0.02]

但是,我处理的当前矩阵非常大,例如m = 100,n = 80,是否有任何构建函数来代替使用for循环?

matlab vector matrix

0
推荐指数
1
解决办法
74
查看次数

编程垂直编码锯齿形矩阵

我正在编写一个以zigzag模式打印nxn矩阵的动态代码.请帮助我获取以下输出的代码:

到目前为止,我在Rizier123的帮助下尝试过的代码是水平锯齿形图案:

#include <stdio.h>

int main() {

    int rows, columns;
    int rowCount, columnCount, count = 0;

    printf("Please enter rows and columns:\n>");
    scanf("%d %d", &rows, &columns);


    for(rowCount = 0; rowCount < rows; rowCount++) {

        for(columnCount = 1; columnCount <= columns; columnCount++) {

            if(count % 2 == 0)
                printf("%4d " , (columnCount+(rowCount*columns)));
            else
                printf("%4d " , ((rowCount+1)*columns)-columnCount+1);

        }
        count++;
        printf("\n");
    }


    return 0;

}
Run Code Online (Sandbox Code Playgroud)

输入:

5 5
Run Code Online (Sandbox Code Playgroud)

输出:

 1  2  3  4  5
10  9  8  7  6
11 12 13 14 …
Run Code Online (Sandbox Code Playgroud)

c matrix

0
推荐指数
1
解决办法
3573
查看次数

为什么我的OpenGL对象相对于最后绘制的对象进行绘制?

我很确定这是因为我对GL_MODELVIEW矩阵的工作方式缺乏了解.这是一个屏幕录制正在发生的事情:http://youtu.be/3F7FLkVI7kA

如您所见,最底部的三角形是第一个绘制的三角形,并且随着我预期其他两个三角形移动而移动.第二个三角形相对于第一个三角形移动和旋转,第三个三角形相对于该组合​​移动和旋转.

我想要的是所有三个三角形在3D空间中是静止的,但是旋转(就像第一个三角形).

资源:

// Main loop
do {
    // Clear Screen
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Update camera
    glfwGetCursorPos(window, &cursorX, &cursorY);
    cam.update(0.001f, (int)cursorX, (int)cursorY);

    // Reset Matrix
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // move camera
    glRotatef(cam.rotation.x, 1.0f, 0.0f, 0.0f);
    glRotatef(cam.rotation.y, 0.0f, 1.0f, 0.0f);

    // translate modelview matrix to position of the camera - everything should now draw relative to camera position
    glTranslatef(-cam.position.x, cam.position.y, -cam.position.z);

    // Draw ground
    drawGroundGrid(-25.0f);
    drawSpinningTriangle(0.0f, 0.0f, -5.0f);
    drawSpinningTriangle(3.14f, 3.0f, -6.0f);
    drawSpinningTriangle(-6.0f, 12.0f, -5.0f);

    // Swap buffers …
Run Code Online (Sandbox Code Playgroud)

c++ opengl matrix glfw

0
推荐指数
1
解决办法
398
查看次数

这个Matlab/Octave代码如何从向量创建一个布尔矩阵?

第一行代码使用"离散标签"创建一些向量,第二行代码创建一个稀疏矩阵,在标签所代表的索引处有一个."eye"会创建一个单位矩阵,但即使向量"a"更长,创建稀疏矩阵的效果仍然有效!?

你能帮我理解发生了什么吗?

octave:4> a = [1 3 5 7 9 2 4 6 8 10]
a =

    1    3    5    7    9    2    4    6    8   10

octave:5> eye(10)(a,:)
ans =

Permutation Matrix

   1   0   0   0   0   0   0   0   0   0
   0   0   1   0   0   0   0   0   0   0
   0   0   0   0   1   0   0   0   0   0
   0   0   0   0   0   0   1   0   0   0
   0   0   0   0   0   0   0 …
Run Code Online (Sandbox Code Playgroud)

arrays matlab matrix linear-algebra octave

0
推荐指数
1
解决办法
370
查看次数

Matlab:对for循环内的列进行算术运算(简单但不可靠!)

我试图表示一个简单的矩阵m*n(我们假设它只有一行!),使得m1n1 = m1n1 ^ 1,m1n2 = m1n1 ^ 2,m1n3 = m1n1 ^ 3,m1n3 = m1n1 ^ 4,.. .m1ni = m1n1 ^ i.换句话说,我试图在矩阵列上迭代n次以在末尾添加一个新的向量(列),使得每个索引具有与第一个向量相同的值但是被提升到其列号的幂ñ.

这是原始载体:

v =
    1.2421
    2.3348
    0.1326
    2.3470
    6.7389
这是第三次迭代后的v:

v = 
    1.2421    1.5429    1.9165
    2.3348    5.4513   12.7277
    0.1326    0.0176    0.0023
    2.3470    5.5084   12.9282
    6.7389   45.4128  306.0329
现在考虑到我是Matlab中的一个总菜鸟,我真的低估了这个看似简单的任务的难度,这花了我差不多一天的调试和网上冲浪找到任何线索.这是我提出的:

rows = 5;
columns = 3;
v = x(1:rows,1);
k = v;
Ncol = ones(rows,1);
extraK = ones(rows,1);

disp(v)

for c = 1:columns
    Ncol = k(:,length(k(1,:))).^c; % a verbose way of …
Run Code Online (Sandbox Code Playgroud)

matlab for-loop vector matrix dimensions

0
推荐指数
1
解决办法
102
查看次数

使用mex将Matlab中的大矩阵传递给C:Matlab崩溃

我编写了一个mex代码,它从matlab代码向C代码发送标量和矩阵.它适用于较小的矩阵.但是,当我尝试传递大的稀疏矩阵(大小~8448 x 3264)时,matlab崩溃并出现以下错误:

我收到以下错误:Matlab遇到内部问题,需要关闭.

                  *Detailed error report*


------------------------------------------------------------------------
         Segmentation violation detected at Mon Feb  9 13:21:48 2015
  ------------------------------------------------------------------------

  Configuration:
    Crash Decoding     : Disabled
    Current Visual     : None
    Default Encoding   : UTF-8
    GNU C Library      : 2.19 stable
    MATLAB Architecture: glnxa64
    MATLAB Root        : /usr/local/MATLAB/R2014b
    MATLAB Version     : 8.4.0.150421 (R2014b)
    Operating System   : Linux 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64
    Processor ID       : x86 Family 6 Model 44 Stepping 2, GenuineIntel
    Software OpenGL    : 1
    Virtual …
Run Code Online (Sandbox Code Playgroud)

c c++ matlab matrix mex

0
推荐指数
1
解决办法
483
查看次数

请准确解释这个等式

我有这个等式:

I = (I - min(I(:))) / (max(I(:)) - min(I(:)));
Run Code Online (Sandbox Code Playgroud)

在哪里I是矩阵,我知道min(I(:))并分别max(I(:))计算I矩阵的最小和最大元素.

当我做一个随机矩阵rand(5,5)或者randi(5,5)在上面的方程式之前和之后我没有看到任何变化:

两个布偶

但是当我在灰度图像上实现这个方程时,结果是二进制图像:

两个布偶

这里的任何人都能完全解释这个等式吗?

I = (I - min(I(:))) / (max(I(:)) - min(I(:)));
Run Code Online (Sandbox Code Playgroud)

matlab image matrix

0
推荐指数
1
解决办法
76
查看次数

使用for循环对子图像进行子采样

我有一个关于对矩阵/图像进行二次采样的快速问题.我只是在使用for和/或while循环时尝试这样做.基本上问题是重申:

function output = subsample(img,2)

output = img(1:factor:end, 1:factor:end);
Run Code Online (Sandbox Code Playgroud)

但是,现在我试图重写函数来执行完全相同的过程但是使用for循环和/或while循环并且不使用两个或更多" :"(冒号)运算符而不使用冒号运算符访问矩阵/向量元素.这是我到目前为止:

function output = subsamplex(img,factor)

[r, c] = size(img);

output = zeros(r/factor,c/factor);

j = 1;

i = 1;

for x = 1:r;

    for y = 1:c;
        j = factor*j-1;
        i = factor*i-1;
        output(j,i) = img(x,y);
    end
end

end
Run Code Online (Sandbox Code Playgroud)

虽然我得到的是图像调整大小,它全是黑色的,我试图弄清楚我哪里出错或者我完全错了.任何正确方向的帮助将不胜感激.

matlab for-loop image image-processing matrix

0
推荐指数
1
解决办法
2013
查看次数

int矩阵中的分段错误

我正在尝试一些基本的C代码,用于定义int带指针的矩阵.

typedef int **Matrix;

Matrix createMatrix(int lines, int columns) {
    int i, j;
    Matrix m = (Matrix) malloc(sizeof(int) * lines * columns);
    for (i = 0; i < lines; ++i) {
        for (j = 0; j < columns; ++j) {
            m[i][j] = 0;
        }
    }
    return m;
}

int main(int argc, char**argv) {
    Matrix m = createMatrix(5, 10);
    // ...
    if (m[2][3] == 20) {
        // ...
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,这些m[i][j]访问会导致分段错误.这有什么不对?太多的星号?

我确信指向int的指针实际上与矩阵相同.

c pointers matrix segmentation-fault

0
推荐指数
1
解决办法
489
查看次数