如何将值附加到2D数组?

时间:2022-09-28 14:39:36

I am new to MATLAB, and I can't fathom this from the documentation.

我是MATLAB的新手,我无法从文档中了解这一点。

function GotData(sender, args)
    interval = args.DataBlock.TimeIntervalInMicroseconds;
    doubles = args.DataBlock.AsDoubleArray();
    x = 0;
    complexCount = length(double(doubles))/2;
    DATA = zeros(complexCount);
    for index = 1:(complexCount-1)
        realnum = doubles(2 * index);
        imagnum = 1i * doubles(2 * index + 1);
        complex = realnum + imagnum;
        x = x + interval;
        DATA(index) = [x complex];
    end
    disp(DATA)
end

I am getting an array of doubles from an event that fires in a .NET assembly. I'm splitting the array up so that each even item (in a 1 based array) is an imaginary number and each odd item is real. Then I create a two item array of the complex number and its interval. I want to then append this 1D array to a 2D array. How do I do that?

我从.NET程序集中触发的事件中获取了一系列双精度数。我正在拆分数组,以便每个偶数项(在一个基于1的数组中)是一个虚数,每个奇数项都是实数。然后我创建一个复数和它的间隔的两项数组。我想将这个1D数组附加到2D数组。我怎么做?

At the moment I'm getting an error: In an assignment A(I) = B, the number of elements in B and I must be the same.. What should I be doing?

目前我收到一个错误:在作业A(I)= B中,B和I中的元素数量必须相同。我应该怎么做?

interval is 1, but can be adjusted.

间隔为1,但可以调整。

2 个解决方案

#1


2  

If you want DATA to be a 2-D array, you need to initialize it and index it as such:

如果您希望DATA是二维数组,则需要对其进行初始化并将其索引为:

% ...(your code)...
DATA = zeros(complexCount-1, 2);   % Initialize DATA to an N-by-2 matrix
% ...(your code)...
    DATA(index, :) = [x complex];  % Add the array to a row of DATA
% ...(your code)...

You can check out these MathWorks documentation links for further information about creating matrices and matrix indexing in MATLAB.

您可以查看这些MathWorks文档链接,以获取有关在MATLAB中创建矩阵和矩阵索引的更多信息。

#2


2  

I was writing the same answer as gnovice, but he fired first. :)

我和gnovice写的答案相同,但他先解雇了。 :)

In addition, if real data correspond to odd items and imaginary to even items, you should change the assignments:

此外,如果真实数据对应于奇数项目和虚构对应偶数项目,则应更改分配:

realnum = doubles(2 * index - 1);
imagnum = 1i * doubles(2 * index);

Anyway I would vectorize the code to avoid for-loop:

无论如何,我会对代码进行矢量化以避免for循环:

%# ... code to get doubles and interval variables
n = numel(doubles);
realnum = doubles(1:2:n)';
imagnum = 1i * doubles(2:2:n)';
x = interval.*(1:numel(realnum)).';
DATA = [x realnum + imagnum];

#1


2  

If you want DATA to be a 2-D array, you need to initialize it and index it as such:

如果您希望DATA是二维数组,则需要对其进行初始化并将其索引为:

% ...(your code)...
DATA = zeros(complexCount-1, 2);   % Initialize DATA to an N-by-2 matrix
% ...(your code)...
    DATA(index, :) = [x complex];  % Add the array to a row of DATA
% ...(your code)...

You can check out these MathWorks documentation links for further information about creating matrices and matrix indexing in MATLAB.

您可以查看这些MathWorks文档链接,以获取有关在MATLAB中创建矩阵和矩阵索引的更多信息。

#2


2  

I was writing the same answer as gnovice, but he fired first. :)

我和gnovice写的答案相同,但他先解雇了。 :)

In addition, if real data correspond to odd items and imaginary to even items, you should change the assignments:

此外,如果真实数据对应于奇数项目和虚构对应偶数项目,则应更改分配:

realnum = doubles(2 * index - 1);
imagnum = 1i * doubles(2 * index);

Anyway I would vectorize the code to avoid for-loop:

无论如何,我会对代码进行矢量化以避免for循环:

%# ... code to get doubles and interval variables
n = numel(doubles);
realnum = doubles(1:2:n)';
imagnum = 1i * doubles(2:2:n)';
x = interval.*(1:numel(realnum)).';
DATA = [x realnum + imagnum];