简体   繁体   English

[Matlab] [Simulink]如何在二维数组中找到特定值?

[英][Matlab][Simulink] How to find a specific value in a 2d array?

I came across a situation where I have to use Simulink. 我遇到过必须使用Simulink的情况。

I have a csv file ( 1000 rows x 6 columns ) that contain data. 我有一个包含数据的csv文件(1000行x 6列)。 I need to search column 6 of my csv file and find the closest match to whatever I'm looking for, and then return columns 1 2 3 4. 我需要搜索我的csv文件的第6列,找到与我正在寻找的最接近的匹配,然后返回列1 2 3 4。

For simplicity, lets go with a smaller version of my problem. 为简单起见,我们可以使用较小版本的问题。

Csv file example Csv文件示例

0  0  0  0 0 20
0 10 15  2 4 30
1 50  2 54 2 40

I search for 21. I find that row 1, column 6, is closest to 21. I then return 0 0 0 0 (the first four columns. 我搜索21.我发现第1行第6列最接近21.然后返回0 0 0 0(前四列。

I search for 34, and find that row 3, column 6 is the closest to 34. I then return 1 50 2 54. 我搜索34,发现第3行第6列最接近34.然后返回1 50 2 54。

Hopefully that explains what I need. 希望这能解释我的需要。

Originally I wrote a working script in matlab using find(..) and it worked, but this has to be done using simulink. 本来我写了使用find(...)在MATLAB工作脚本和它的工作,但是这必须使用Simulink来完成。

I noticed that there is the 2d loopup table block which can work, however, it appears I need to know my indexes before hand. 我注意到有2d循环表块可以工作,但是,看起来我需要事先了解我的索引。

If anyone can point me in the right direction of what blocks I can use, and i can google around for how to use them, it would be much appreciated. 如果有人能指出我可以使用哪些块的正确方向,并且我可以谷歌周围的如何使用它们,将非常感激。

As I said in my comment, your best bet is to simply make use of the Matlab Function or Interpreted Matlab Function blocks in order to incorporate your already existing function into your Simulink model (no need to reinvent the wheel if you don't have to). 正如我在评论中所说,最好的办法是简单地使用Matlab函数解释的Matlab函数块,以便将现有函数合并到Simulink模型中(如果不需要,则无需重新发明轮子) )。

If you're working under the constraint that this must be "all Simulink" (ie no user-defined blocks), then a few blocks that will probably be helpful are Selector , Find , Min , and Compare To Zero . 如果你的工作条件必须是“所有Simulink”(即没有用户定义的块),那么可能有用的几个块是SelectorFindMinCompare To Zero

One approach that I might take is to use a selector to isolate that 6th column. 我可能采取的一种方法是使用选择器来隔离第6列。 Find the "error" between each value in the column and the value that you are trying match (ie take the absolute value of the difference). 找到列中每个值与您尝试匹配的值之间的“错误”(即获取差异的绝对值)。 Use the Min block to find the minimum error. 使用Min块查找最小错误。 Subtract that min error from your vector of errors so that the index of the closest match will now have a value of 0. Using Compare To Zero and Find you should then be able to determine the index of the closest match. 从误差向量中减去最小误差,以便最接近匹配的索引现在具有值0.使用“ Compare To Zero和“ Find您应该能够确定最接近匹配的索引。 Once you have that, you can use it to drive another Selector block in order to select elements as desired. 完成后,您可以使用它来驱动另一个Selector块,以便根据需要选择元素。

There are other blocks that I can think of that might be really helpful ( Find Local Maxima is something that comes to mind that I bet you could incorporate into a solution); 我能想到的其他一些块可能真的很有帮助( Find Local Maxima是我想你可以融入解决方案的想法); however, they require additional toolboxes. 但是,它们需要额外的工具箱。

You can achieve this using a Minimum block. 您可以使用Minimum块来实现此目的。 Set it's mode to index so that it outputs the index of the item having minimum error. 将其模式设置为索引,以便输出具有最小错误的项目的索引。

Pass this index into a Variable Selector block, taking the original rows/columns via In1 and the index of the minimum on Idx . 将此索引传递到变量选择器块,通过In1获取原始行/列,并在Idx上获取最小值的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM