首页 \ 问答 \ 从逻辑数组掩码(MATLAB)映射的另一个较小矩阵的值构建稀疏矩阵?(Building a sparse matrix from another smaller matrix's values mapped by a logical array mask (MATLAB)?)

从逻辑数组掩码(MATLAB)映射的另一个较小矩阵的值构建稀疏矩阵?(Building a sparse matrix from another smaller matrix's values mapped by a logical array mask (MATLAB)?)

我正在使用大型稀疏矩阵( 稀疏 )。 我有一个大的稀疏值矩阵,需要包含在一个更大的稀疏矩阵中。 我有一个logicals数组,指示哪些行和列要用较小的矩阵值填充。 在该应用中,两个矩阵中较小的一个是存储为邻接矩阵的图,逻辑表示较大矩阵中的节点id位置。

一个小玩具示例来展示我目前正在做的事情:

zz = sparse(4,4);  %create a sparse matrix of the final size desired
rr = rand(3,3);   %a matrix of values
logical_inds = logical([1 0 1 1]); %a logical array to index with the dimension size of zz
zz(logical_inds,logical_inds) = rr(:,:) %'rr' values are mapped into the subset of 'zz'

我看到zz 第二列是零, 第二行也是零值。 这是所需的输出。

在我的程序中得到一个警告,这个“稀疏索引可能会很慢”,而且确实如此。 有时,当矩阵非常大时,程序终止于此行。

如何使用稀疏方法创建此矩阵( zz )? 我不确定如何从我拥有的逻辑掩码创建行列索引,以及如何将rr的值转换为适合此新索引的数组。

**一般来说rr非常稀疏,尽管逻辑掩码解决了整个矩阵


I am working with large sparse matrices (sparse). I have a large sparse matrix of values which needs to be included into a larger sparse matrix. I have an array of logicals which indicates which rows and columns are to be filled up with the smaller matrix values. In this application the smaller of the two matrices is a graph stored as an adjacency matrix and the logicals indicate the node id positions in the larger matrix.

A small toy example to demonstrate what I am doing currently:

zz = sparse(4,4);  %create a sparse matrix of the final size desired
rr = rand(3,3);   %a matrix of values
logical_inds = logical([1 0 1 1]); %a logical array to index with the dimension size of zz
zz(logical_inds,logical_inds) = rr(:,:) %'rr' values are mapped into the subset of 'zz'

I see that the 2nd column of zz are zeros, and that the 2nd row are zero values as well. This is the output desired.

In my program get a warning that this "sparse indexing is likely to be slow", and it is. Occasionally when the matrices are very large the program terminates at this line.

How can I create this matrix (zz) with the sparse method? I am unsure how to create the row column indexes from the mask of logicals I have, and how to turn the values of rr into an array ordered appropriately for this new indexing.

**in general rr is very sparse although the mask of logicals addresses the full matrix


原文:https://stackoverflow.com/questions/24555240
更新时间:2023-10-09 13:10

最满意答案

如果我理解你的正确,你的小部件将需要他们自己的动作控制器,这是他们获取数据的逻辑,解析表单提交等应该去。 在这种情况下,窗口小部件和页面之间的区别在于它是如何呈现的,即作为HTML片段而不是整个页面; 您可以使用Action View Helper来实现此目的。

如果您的窗口小部件包含表单,则应该使用AJAX将表单数据提交回服务器,这样使用窗口小部件不会导致用户意外离开页面。 您可以通过在窗口小部件的视图和/或操作中使用头脚本助手 ,将所需的JavaScript注入到已包含窗口小部件的页面中。


I left Richards reply, the problem, and further use cases cook in my brain for a while longer and ended up coming to a solution.

I will have the following view helpers and methods:

Content; with methods: render, renderWidgets, renderWidget, renderCommentsWidget (comments).
Event; with methods: renderEventsWidget (many events), renderEventWidget (one event)
Subscription; with methods: renderSubscribeWidget (subscription form).

I will have inside my configuration file:

app.widgets.comments.helper = content
app.widgets.subscribe.helper = subscription
app.widgets.events.helper = event

I will also have the following models:

Content for use for all pages.
Event for use for all events.
Subcriber for use for subscriptions to content

So inside my view I will do something like this: echo $this->content()->render($this->Content)

Content::render() will then perform any content rendering and then perform rendering of the widgets by passing along to Content::renderWidgets(). Here we will use the configuration of app.widgets to link together the widget bbcode tag to it's appropriate view helper (using the naming convention 'render'.ucfirst($tag).'Widget'). So for example Content::renderCommentsWidget() would then proceed to render the comments.

Perhaps later on I will decide to have a Widget View Helper, and individual view helpers for each widget eg. ContentCommentsWidget View Helper. But for now that would just add additional unrequired complexity.

Now to answer the AJAX problem I mentioned. Say for the comments widget allowing for comments to be posted via ajax. It would simply have an appropriate method inside the Content Controller for it. So pretty much we also have a Event and Subscription controllers too - corresponding with the view helpers. Interaction between the view helper and controller will all be hard coded, there is no purpose for it to be soft coded.

I hope this helps someone else, and the current plan is to make the project where all this is used to be an open-source project. So maybe one day you can see it all in action.

Thanks.


Update:

You can find the source code of these ideas in action in the following repositories:

  • BalCMS - this is the actual CMS which contains the widgets in /application/modules/balcms/view/helpers and contains the configuration in /application/modules/config/application/balcms.yaml
  • BalPHP - this is the resource library which contains the widget view helper at /lib/Bal/View/Helper/Widget.php

相关问答

更多
  • 我修好了整件事。 我发现上传必须作为资源添加。 使用可在此处找到的插件修复了Flash会话问题 注意:目前仅对本地主机进行了测试 这是脚本的外观: index.phtml