Build new matrix from look up table

1 vue (au cours des 30 derniers jours)
Tapashree Roy
Tapashree Roy le 6 Sep 2019
Commenté : Tapashree Roy le 20 Sep 2019
Hello,
I have a column vector A, corresponding to which I need to fill a vector C, the same size as A.
The value of C would be assigned from the "look up" table B-CD.
For the closest matching value of B to each element in A, the corresponding CD will be filled in to C.
So for the example below, for A= 0.35, 0.95, the C should be 200, 400.
How do I assign correct values to C?
%This code does not really work
A=[0.35 0.95];
C = zeros(length(A),1);
B=[0.1 0.4 0.8 1];
CD=[100 200 300 400];
minDiff =0.5;
for ii = 1:length(B)
diff = abs(B(ii) - A);
if(diff < minDiff)
B_index = ii;
minDiff = diff;
C=CD(B_index)
end
end

Réponse acceptée

Catalytic
Catalytic le 6 Sep 2019
C=CD( interp1(B,1:numel(B),A,'nearest') );
  4 commentaires
Tapashree Roy
Tapashree Roy le 6 Sep 2019
Stephen, better still! Thanks!
Tapashree Roy
Tapashree Roy le 20 Sep 2019
Hi, Follow up question: Now that C is found, how can I find the corresponding B?
i,e.: for this example, when C = 200 400, BC=B would be BC= 0.4 1.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by