Storing data from a 1-D interpolation with repeated values

10 views (last 30 days)
Hello,
I am having troubles trying to store data from a 1-D interpolation with repeated values. Let have the following matrix:
a1= [1 3 4
2 5 7
3 6 8
3 9 9]
I would like to interpolate by columns the following vector:
b=[1.5 6.4 7.9]
Interpolating 1.5 on the first column, 6.4 on the second one, and 7.9 on the last one. We know we cannot interpolate when there are repeated values as it happens on the first column. I decided to use the function unique, and adapt it a bit, as it is prepared to erase data in rows instead of columns.
I can do it for one column:
a2=unique(a1(:,1)','stable')'
But when I want to do it for all columns (in my case I have a 100x50-Matrix and there are other columns with repeated values), as "unique" delete those repeated values, it does not let me store them all in a matrix, retrieving the obvious error that dimensions do not match.
for i=1:50
a2=unique(a1(:,i)','stable')';
a(:,i)=a2;
end
Is there a way to store "a" as a structure or cell-array instead of a matrix? I have tried it out but I may be mispelling the code.
Thank you,
John
  3 Comments
Walter Roberson
Walter Roberson on 29 May 2015
What reason would we have to expect that a2c{1,i3} will just happen to be the right length? You get rid of an unknown number of entries to form a1c{1,i2} so what causes a2c{1,i3} to be just the right size?
Also you should be using cell2mat(Interp_data) not on b.
You could call matcell() to do the splitting. That won't be any more efficient as it does the for loop itself; it would just make the code smaller.
John Mathew
John Mathew on 29 May 2015
Edited: John Mathew on 29 May 2015
Hello Walter, Thank you for your answer. Well, I am relatively new to Matlab, but I would say, if a2c does not have the same length than a1c, you are not allowed to do the interpolation.
I forgot to add, that I converted a2c from matrix to cell as I did with a1c. I will edit it now.
Thank you for your suggestion, but I did not manage to use matcell() in a way that I could get a 1-row cell with 50 columns, wherein each column stores a 100x1-vector. I was getting a cell{1,1} instead where a matrix [100x50] was stored. That is why I used the for-loop.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 28 May 2015
cols_to_uniquify = [1 2 9 43:50];
A2 = A1;
for K = cols_to_uniquify
[~, uaidx] = unique(A2(:,K), 'stable');
A2 = A2(uaidx, :);
end
This will delete any row in which a value appears on column K which has already appeared earlier in column K, operating over the given list of columns.
I did not write this to go over all of the columns because interpolation requires source values ("x") and corresponding values ("y") and a list of x locations to predict y for. You thus need to maintain a correspondence between x and y values that you are keeping. If you are running this uniquing process over all columns it would imply that they are all "x" type values and that you have no corresponding "y" values. I thus deduce that at least one of your columns must be a "y" list that should not be unique()'d because you are not going to be doing interpolation with it as source values.
It could happen that the kind of interpolation you are doing is not linear interpolation but instead is a question of finding the "nearest" value in the list, or possibly a question of finding the "greatest value that does not exceed" that is in the list. If that is what you are doing, then Yes, you might want to run the process over all of the columns -- but if it is what you are doing then interp1() might not be the best routine to use. For example for "not to exceed", histc() is usually a better alternative.

Products

Community Treasure Hunt

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

Start Hunting!