MATLAB:定义矩阵的 n 个子集

发布于 2024-11-19 04:58:39 字数 499 浏览 2 评论 0原文

我有一个 1974x1 向量 Upper,我试图将信息分解为每个包含 36 个项目的单独数组。因此,我使用 length 发现有 1974 个项目,然后除以 36 并使用 floor 函数。我不知道如何用 n 完成这一切。

这是我的逻辑:我定义 n 是为了找到需要定义的子集的数量。然后,我试图让subsetn 成为subset1、subset2、...、subset36。然而,MATLAB仅将矩阵subsetn定义为1x36矩阵。然而,该矩阵包含subset1 应该包含的内容(1...36)。你们对新手有什么建议吗?我做错了什么?

binSize = 36;
nData = length(Upper);
nBins = floor(nData/36);
nDiscarded = nData  - binSize*nBins;

n=1:binSize;
subsetn= [(n-1)*binSize+1:n*binSize];

I have a 1974x1 vector, Upper, and I am trying to break the information up into individual arrays of 36 items each. So, I used length to find that there are 1974 items and then divided by 36 and used the floor function. I cannot figure out how to do it all with n.

Here is my logic: I am defining n in an attempt to find the number of subsets that need to be defined. Then, I am trying to have subsetn become subset1, subset2,...,subset36. However, MATLAB only definies the matrix subsetn as a 1x36 matrix. However, this matrix contains what subset1 is supposed to contain(1...36). Do you guys have any advice for a newbie? What am I doing wrong?

binSize = 36;
nData = length(Upper);
nBins = floor(nData/36);
nDiscarded = nData  - binSize*nBins;

n=1:binSize;
subsetn= [(n-1)*binSize+1:n*binSize];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2024-11-26 04:58:39

您可以创建一个 54x36 数组,其中第 n 列是您的第 n 子集。

subsetArray=reshape(x(1:binSize*nBins),[],nBins);

您可以通过 subsetArray(:,n) 访问第 n 个子集

You can create a 54x36 array where the nth column is your nth subset.

subsetArray=reshape(x(1:binSize*nBins),[],nBins);

You can access the nth subset as subsetArray(:,n)

嗳卜坏 2024-11-26 04:58:39

如果我误解了你想要做什么,请提前抱歉。

我认为下面的小技巧可能会达到您想要的效果(这很hacky,但我不是Matlab专家):

[a, b] = meshgrid(0:nBins-1, 0:binSize-1)
inds = a*binSize + b + 1

现在inds是一个nBins*binSize索引矩阵。您可以使用它对 Upper 进行索引,这样

Upper(inds)

您就可以将子集作为结果矩阵中的列。

编辑:看到尤达的答案,他的更好;)

Sorry in advance if I misunderstood what you want to do.

I think the following little trick might do what you want (it's hacky, but I'm no Matlab expert):

[a, b] = meshgrid(0:nBins-1, 0:binSize-1)
inds = a*binSize + b + 1

Now inds is a nBins*binSize matrix of indices. You can index Upper with it like

Upper(inds)

which should give you the subsets as the columns in the resulting matrix.

Edit: on seeing Yoda's answer, his is better ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文