简体   繁体   中英

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

I came across a situation where I have to use Simulink.

I have a csv file ( 1000 rows x 6 columns ) that contain data. 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.

For simplicity, lets go with a smaller version of my problem.

Csv file example

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.

I search for 34, and find that row 3, column 6 is the closest to 34. I then return 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.

I noticed that there is the 2d loopup table block which can work, however, it appears I need to know my indexes before hand.

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).

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 .

One approach that I might take is to use a selector to isolate that 6th column. 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. 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. Once you have that, you can use it to drive another Selector block in order to select elements as desired.

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); however, they require additional toolboxes.

You can achieve this using a Minimum block. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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