MATLAB: If the value from the 1st column in one matrix is between two 1st column values from another matrix, return the row values from the second matrix.

for loopif statement

I want to assign weighting values to geolochemical samples contained within matrix "rawdata" based on the latitude where the rocks were collected (latitude is in 1st column, chemical data in remaining columns). To do this, I have a matrix "Weight_Latbin" where the 1st column has latitude values and the other columns contain weighting values that should be assigned to any sample collected between that lat and the latitude of the next row. These would be in a new matix called "Weights_all" (the ist column would keep the latitude value of the 1st column of rawdata).
For example (simplified):
raw data= [40.55 .. .. ; 40.33 .. ..;....]
Weight_Latbin= [40.25 0.80 0.75; 40.50 0.66 0.40; 40.75 0.25 0.90; ...]
Because the latitude of the 1st row of rawdata(1,1)=40.55 is BETWEEN Weight_latbin(2,1)=40.5 and Weight_latbin(3,1)=40.75, I want the 1st row of Weights_all(1,2:end) to have the values of Weight_Latbin(2,2:end).
In the end, for this example, Weights_all= [40.55 0.66 0.40; 40.33 0.80 0.75;..].
This is what I tried:
Weights_all=zeros(size(rawdata));
for ii=1:size(rawdata,1)
for jj=1:size(Weight_Latbin,1)
if rawdata(ii,1)>=Weight_Latbin(jj,1)& rawdata(:,1)<Weight_Latbin(jj+1,1)
Weights_all(ii,2:size(Weight_Latbin,2))=Weight_Latbin(jj,2:size(Weight_Latbin,2));
end
end
end
I am very new to matlab, and I can't get this to work! Please help! (using Matlab 7.10.0 R2010a)

Best Answer

weights=cell2mat(arrayfun(@(x) Weight_Latbin(find(x>Weight_Latbin(:,1),1,'last'),2:end),rawdata,'uniform',false))
weights =
0.6600 0.4000
0.8000 0.7500
>>