loop error with "unique" and "interp1"
조회 수: 7 (최근 30일)
이전 댓글 표시
Dear Matlab-Experts,
trying to find the 50% threshold for a sigmoid shaped curve using interp1 for a set of subjects. Managed to do single subjects here:
load cgft
load cgsx
size(cgft)
size(cgsx)
nsub = 8;
[~, ind1] = unique(cgft); % ind = index of first occurrence of a repeated value
yc(nsub, :) = interp1(cgft(ind1), cgsx(ind1), .50, 'linear','extrap');
But my loop for all 53 subject fails:
% for all subjects:
load cgft
load cgsx
subjects = [1:53];
nbsubjects = length(subjects);
for nsubs = 1:nbsubjects
nsub = subjects(nsubs);
[~, ind1] = unique(cgft(nsub, :));
yc(nsub, :) = interp1(cgft(ind1(nsub, :)), cgsx(ind1(nsub, :)), .50, 'linear','extrap');
end
Would anyone know why the loop doesn't run please? (I'm a matlab dabbler...not a regular user...)
(Error using matlab.internal.math.interp1
Interpolation requires at least two sample points for each grid dimension.)
댓글 수: 3
채택된 답변
Star Strider
2023년 4월 30일
To avoid all the problems about having unique values in one of the vectors, select a narrow range for the interpolation. That (nearly always, in most instances, and always here) prevents that problem.
Try this —
LD1 = load('cgsx.mat');
cgxs = LD1.cgsx;
LD2 = load('cgft.mat');
cgft = LD2.cgft;
for k = 1:size(cgxs,1)
% qv = cgft(k,:)
% Q = [max(cgft(k,:)); min(cgft(k,:))]
val50(k) = 0.5*(max(cgft(k,:))-min(cgft(k,:)))+min(cgft(k,:)); % Detect 50% Of The Range Of A Specific 'cgft' Vector
ix50 = find(diff(sign(cgft(k,:)-val50(k)))); % Approximate Index Of That Value
idxrng = max(1,ix50-1) : min(numel(cgxs(k,:)),ix50+1); % Index Range For Interpolation
cgxs50(k) = interp1(cgft(k,idxrng), cgxs(k,idxrng), val50(k)); % Interpolate On That Range
end
midrange_and_cgxs_values = [val50; cgxs50]
figure
plot(cgxs.', cgft.')
% plot(cgxs(:,[1 2]), cgft(:,[1 2]))
% plot(cgxs([1 2],:).', cgft([1 2],:).')
hold on
plot(cgxs50, val50, 'sr')
hold off
grid
xlabel('cgxs')
ylabel('cgft')
If you want to strictly interpolate on the ‘global’ y-value of 0.5, replace that with ‘val50’ in my code. My code scales the 50% value with the ranges of the individual ‘cgft’ vectors.
.
댓글 수: 2
Star Strider
2023년 4월 30일
As always, my pleasure!
I’m sure you would have, just as I did, when the need arose. I developed that approach when I neded to interpolate a quasi-periodic function and had to define individual interpolatioon regions for each time the signal crossed a specific threshold. It very nicely generalises to other sorts of problems, and actually seems to make the code more efficient because it narrows the interpolation region.
추가 답변 (1개)
Walter Roberson
2023년 4월 30일
cgft(nsub, :) is all the same value so the second output of unique is only a single entry
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
