finding value in 2d array function of x and y
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 1×100 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 1×1000. 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:
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 clearHi 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 1×100 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 1×1000. 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:
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 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 1×100 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 1×1000. 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:
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 map, array, efficiency, dutycycle, script MATLAB Answers — New Questions
​