Tyler Ram
Tyler Ram

Reputation: 13

Matlab How to sort through and map IVs to unique H values

H = [500 500 500 450 450 450 400 400 350 350 350 300 300 300];

I = [-0.0019 -0.0018 -0.0017 -0.0019 -0.0018 -0.0017 -0.0019 -0.0018 -0.0017]

V = [-7.54E-06 -7.23E-06 -6.93E-06 -7.53E-06 -7.21E-06 -6.89E-06 -6.60E-06 -7.50E-06 -7.23E-06 -6.90E-06]

Need to sort through the data and get an IV for each value but lump then off the uniqueness of their value, e.g., I want all the IVs for the H at 400, etc. I know there is a unique function in Matlab, but that only gives me a single element of the array that is unique. Is there a way to loop through the unique values to get the IV values? I already know about the unique function in Matlab. Thanks!

Upvotes: 0

Views: 39

Answers (1)

gnovice
gnovice

Reputation: 125854

Assuming your vectors have the same number of elements (i.e. every value in H has a corresponding value in I and V), one way you can do this is by using unique to get index values for groupings in H and splitapply to collect I and V values in a cell array based on that index:

>> I = [I 0 0 0 0 0]; V = [V 0 0 0 0];  % Pad your sample data with zeroes at the end
>> [Hvalues, ~, index] = unique(H);
>> IV = splitapply(@(x) {x}, [I(:) V(:)], index)

IV =

  5×1 cell array

    [3×2 double]
    [3×2 double]
    [2×2 double]
    [3×2 double]
    [3×2 double]

>> IV{Hvalues == 400}  % I (first column) and V (second column) values for H = 400

ans =

  -0.001900000000000  -0.000006600000000
  -0.001800000000000  -0.000007500000000

Upvotes: 1

Related Questions