finding value in 2d array function of x and y
4 views (last 30 days)
Show older comments
Hi all, I realise this may be the first time I ask a question even though I've been using the forum for 12 years. Kudos to you for asking every question and answering everything that ever crossed my mind. Here's my question:
Input data:
- I have two vectors, Torque effmap.chainTQ and Speed effmap.chainRPM. They are two columns 1x100 elements
- There is also a 2D array, much like below. For every (Torque,Speed) pair, there is a corresponding Efficiency value effmap.chainRPM
Processing:
- I have a new pair of vectors, say vehicle.wheeltorque and vehicle.wheelspeed which are 1x1000. They represent a duty cycle, which the vehicle is going thru.
- For every set of (vehicle.wheeltorque ,vehicle.wheelspeed) I need the code to find the closest value of Efficiency. I can't figure this one out. I suppose it should be a double for loop of sorts and I found array indexing might be needed, but so far I couldn't get it to work.
Example efficiency map:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1712976/image.jpeg)
What I tried, and works, is to take the base efficiency map, use interp2 and meshrid to refine it, and for a given single set of (testspeed,testtq) I can extract the Efficiency value - see the "point" value:
effmap.chainRPM = xlsread ('03b_DummychainLossmap','Chain', 'B3:B23')
effmap.chainTQ = xlsread ('03b_DummychainLossmap','Chain', 'C2:W2')
effmap.chainLoss = xlsread ('03b_DummychainLossmap','Chain', 'C3:W23')
surf(effmap.chainRPM,effmap.chainTQ,effmap.chainLoss)
[MeshX,MeshY] = meshgrid(0:10:2000,0:10:2000) %mesh to create more points in a map/array
MeshQ = interp2(effmap.chainRPM,effmap.chainTQ,effmap.chainLoss,MeshX,MeshY,'spline');
surf(MeshX,MeshY,MeshQ)
testspeed = 200
testtq = 1000
point = interp2(MeshX,MeshY,MeshQ,testtq,testspeed) %so now I know it works
However, it doesn't work for a full vector of torque and speed :
linear_indices = sub2ind(MeshQ, MeshY(1,:), MeshX(1,:))
logical_indices = (MeshQ(:, 1) == MeshX & MeshQ(:, 2) == MeshY)
extracted_values = MeshQ(logical_indices, :)
for vehicle.wheeltorque(1:) to vehicle.wheeltorque(1:size(vehicle.wheeltorque))
....................
I tried it in many ways, but fundamentally I don't understand what the logic of the code should be. Help? Last thing, it needs to store the efficiency values in a vector for further analysis.
Cheers, ask if not clear
0 Comments
Answers (1)
Torsten
on 10 Jun 2024
Edited: Torsten
on 10 Jun 2024
Given (vehicle.wheeltorque ,vehicle.wheelspeed), use pdist2 to compute the distances to the column vectors of the matrix [Torque effmap.chainTQ ;Speed effmap.chainRPM ].
Get the index idx where the distance is minimal and assign effmap.chainRPM(idx) as efficiency to (vehicle.wheeltorque ,vehicle.wheelspeed).
This is some kind of "nearest neighbour" interpolation.
You could also try "scatteredInterpolant"
but I'd suggest starting with the simplest method described above.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!