How to use an array as an indexing table to add search result as a new row to the querying array ?

1 view (last 30 days)
Hi, :
I thank you at first.
My question is , 'How to use an array as an indexing table to add search result as a new row to the querying array ?'
For example, the Q matrix is 1000 x 1, the Idx matrix is 5 x 3, as below:
Idx =
1 2 10
2 3 20
3 4 30
4 5 40
5 6 50
Then use Q( : ) every cell to search Idx by the logical expression, " if Idx(1 , 1 , : ) < Q(:,1) < Idx(1, 2 , :) "
,to search which row will the Q(:) be located, then return the columns 3 of the Idx(,,3) respectively to store every it in Q by added a new column 2.
For example Q(1) = 3.5, then Idx(3, 1) = 3, Idx(3, 2) = 4, so that " Idx(3, 1) < Q(1) < Idx(3, 2)", so the Q(1) should have the returned result from Idx's column3 = 30, then store this '30', to a new column2 to Q(1,2) = '30'
Is it possible to avoid using loop, or any existing similar function to simplify it ?
Thank you very much.
Best regards.

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 24 Jul 2019
Edited: Andrei Bobrov on 24 Jul 2019
Q_new = [Q,Idx(discretize(Q,[Idx(:,1);Idx(end,2)]),3)];
  9 Comments
Tamura Kentai
Tamura Kentai on 24 Jul 2019
Hi, Andrei:
I beg your pardon.
I discovered it after I tried some successfully.
Is it possible, to get an interpolating value between 2 index ?
For example, if the indexing value of Q(1) is 3.5, which is the middle value of the row4 & row5, then return the value (40 -30) * ( (3.5 -3) / (4-3) ) + 30 = 35 .
Thank you very much.
Best regards.
>> [Inew(:,1) Inew(:,3)]
ans =
-Inf NaN
1 10
2 20
3 30
4 40
5 50
6 NaN

Sign in to comment.

More Answers (1)

Tamura Kentai
Tamura Kentai on 24 Jul 2019
Hi, Andrei:
I'm sorry, after some hours, I think I have one solution. It's the following:
% index_array_search.m
load('Idx.mat');
load('Q.mat');
Idx = [(1:5)',(2:6)',10*(1:5)'];
Inew = [-inf,1,nan;Idx;6,inf,nan];
[~,ii] = histc(Q,[Inew(:,1);Inew(end,2)]);
out = [Q,Inew(ii,3),Inew(ii+1,3), Inew(ii,1), Inew(ii+1, 1)];
out = [out,( ( out(:,1)-out(:,4) ).*( out(:,3) - out(:,2) ) ./ (out(:,5)-out(:,4)) + out(:,2) )];
I add those necessary for computing into the array.
It looks pretty fine.
Thank you for the ideas.
Best regards.
>> index_array_search
>> out
out =
1.1000 10.0000 20.0000 1.0000 2.0000 11.0000
2.2000 20.0000 30.0000 2.0000 3.0000 22.0000
3.3000 30.0000 40.0000 3.0000 4.0000 33.0000
4.4000 40.0000 50.0000 4.0000 5.0000 44.0000
5.5000 50.0000 NaN 5.0000 6.0000 NaN
  3 Comments
Tamura Kentai
Tamura Kentai on 25 Jul 2019
Hi, Andrei:
In fact, I am thinking if there are other options other than 'linear', actually I mean it's 'linear' distribution in log-log plane.
For example, if plot the 'y=-x' using 'loglog(x,y)', the line will be straight, but if add some constant offset, eg: 'y=-x + 500', the plot 'loglog(x,y)', it won't be straight line in log-log plane.
If you think it's interesting. In fact I have tried survey ,but no good result except using manually adjusting such math equations.
Thank you very much.
Best regards.

Sign in to comment.

Products


Release

R2013b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!