Equivalent to a Vloookup from Excel - can it be done?
1 view (last 30 days)
Show older comments
Obviously it can be done, MatLab can do almost anything, I just can't, hence why I'm asking.
I have two separate arrays: Array A and Array B, each containing 500,000 values in the one column. Array A ranges from 0:265, while Array B ranges from 0:5000, both in a random order with values repeating etc etc.
I'm wanting eventually to plot a nice graph that, along the x-axis, goes 0:1:265 (i.e. Array A), and along the y-axis contains the average value of Array B, 0:1:5000.
In order to do this I'm trying to get a loop that runs through both the arrays and does something like:
- Finds all the values of ArrayA that are between 0:1.
- Take the corresponding values from ArrayB at the same points.
- Average the values taken from ArrayB.
- Then move onto finding the values in ArrayA that are between 1:2.
- Repeating all the way up to ArrayA between 264:265.
- Then plot the average values of ArrayB for each of the values of ArrayA.
I could do it in Excel with a VLookup function or an index match function, but the data isn't in Excel. I could make MatLab write the data to Excel, but that A. isn't the point, and B. would take Excel forever to go through.
Any help would be appreciated greatly!
0 Comments
Answers (1)
John D'Errico
on 29 Apr 2017
1. Take the floor of your array A. That will convert the elements to integers, 0 - 265.
2. Add one, since these will be used as indexes, and MATLAB uses 1-based indexing.
3. Read the help for accumarray. Use it, specifying @mean as the function to be used for accumulation.
Alternatively, you could have used ceil. It all depends on whether any elements that were EXACTLY an integer already (like 2) belong in the bin from 1 to 2, or the bin from 2 to 3. So the choice of floor or ceil will impact where things get sent.
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!