如果数组被认为是水平或垂直结构 [英] Should arrays be thought of as horizontal or vertical structures

查看:137
本文介绍了如果数组被认为是水平或垂直结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些Matlab的功课,我是有问题的概念化,它涉及矩阵的方式。在MATLAB中矩阵是 D(行,列)格式。

I am working on some Matlab homework and I was having issues conceptualizing the way that it addresses matrices. In Matlab the matrix is address in d(row,col) format.

我一直在编程了一会儿,一直倾向于认为一维数组与第二维从下延伸出来的水平结构。

I have been programming for a while and have always tended to think of a one dimensional array as a horizontal structure with a second dimension extending out from below.

哪一个是从电脑的角度考虑的阵列的更正确的方式。

Which of these is a "more correct" way of thinking about an array data structure from the computer's point of view

推荐答案

问得好+1。

单纯从一个Matlab编程的角度看,最好是想一个矩阵列向量的序列。为什么?因为这是怎么Matlab的它们分配到你的计算机上的内存。即,在矩阵的任何给定的列两个连续元素将被在存储器彼此相邻分配。这有时被称为列主顺序,并在语言如Fortran的,R和Julia被使用。与此相反的是,勿庸置疑,被称为行优先的顺序,并在C和Python的使用。

Purely from a Matlab programming perspective, it is best to think of a matrix as a sequence of column vectors. Why? Because this is how Matlab allocates them to your computers memory. That is, two sequential elements in any given column of a matrix will be allocated next to each other in memory. This is sometimes referred to as "column-major order", and is used in languages such as Fortran, R, and Julia. The opposite is, unsurprisingly, called "row-major order", and is used in C and Python.

此的含义是,Matlab的将在上比在行的矩阵的列进行操作快得多。 @angainor提供的<一个href=\"http://stackoverflow.com/questions/12522888/arrayfun-can-be-significantly-slower-than-an-explicit-loop-in-matlab-why\">great回答以几个月前我的一个问题,证明了这一事实。根据@ angainor的洞察,这里是一个有用的速度测试运行:

The implication of this is that Matlab will be much faster at performing operations on the columns of a matrix than on the rows. @angainor provided a great answer to a question of mine a few months ago that demonstrates this fact. Based on @angainor's insight, here is a useful speed test to run:

M = 1000; %# Number of iterations over each method
T = 1000; %# Number of rows
N = 1000; %# Number of columns

X = randn(T, N); %# Random matrix

%# Loop over the rows of a matrix and perform a sum operation on each row vector
tic
for m = 1:M
    for t = 1:T
        sum(X(t, :));
    end
end
toc

%# Loop over the columns of a matrix and perform a sum operation on each column vector
tic
for m = 1:M
    for n = 1:N
        sum(X(:, n));
    end
end
toc

在我的机器上,测试的结果是:

On my machine, the outcome of the test is:

Elapsed time is 9.371870 seconds. %# Looping over rows
Elapsed time is 1.943970 seconds. %# Looping over columns

在换句话说,在列上执行的操作比对行进行的操作速度几乎5倍!

In other words, operations performed on columns are almost 5 times faster than operations performed on rows!

从数学的角度来看,我不相信我自己给一个很好的答案。你也许可以得到一些伟大的见解 math.stackexchange

From a mathematical perspective I don't trust myself to give a good answer. You could probably get some great insights from math.stackexchange.

这篇关于如果数组被认为是水平或垂直结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆