Align curves along their minma

Hi all,
I have a matrix (33X 5000) and each row of this matrix is a curve. I want to center all the curves along their minima, which I have already found and it is the column vector m=(51X1). I tried with a loop but it is not working. Can you help me with that? thanks .

7 Comments

We have to see what you tried to be able to tell you how to fix it.
b is (33X5000) matrix and center is mine column vector with the minima of every row of the matrix (33X1)
for n=1:size(b,1)
b(n,:)=((b(n,:)))-center(n,:);
%l2{n,:}=(1:(length(b{n,:})))+l(n);
b_nor(n,:)=b(n,:)/max_first(n,:);
end
Why is the (badly-named) m 51 rows while the matrix is 33 rows? I thought you'd have one min index for each of the 33 rows of the matrix? So m should have 33 rows, not 51.
What do you mean by 'center' all the curves along their minima?
I am sorry @Image Analyst I was writing the question on the train, The correct matrix and vector (with a nicer name) are wriiten in my answer to Matt. Anyway the vector is center=(33X1).
@Paola I think my code below in my answer should solve it, right?
Thank you very much @Image Analyst. It works perfectly :)

Sign in to comment.

 Accepted Answer

Try this:
% Create sample data.
b = rand(33, 5000);
% Find the location (index) of the min in each row of b.
[minValues, indexOfMins] = min(b, [], 2);
[rows, columns] = size(b)
% Allocate space to place the aligned vectors.
bCentered = nan(rows, 2 * columns - 1);
% Shift each row of b such that the min occurs at column "columns".
for row = 1 : rows
% Find out where the start of this row should begin in the output matrix.
startingColumn = columns - indexOfMins(row) + 1;
% Paste it there.
bCentered(row, startingColumn : startingColumn + columns - 1) = b(row, :);
end
% Now they're all aligned with the min showing up at column "columns"
% which is in the middle of the bCentered array.
% Optional : If you want to crop off columns of all nans to the right and left, do this:
goodColumns = ~all(isnan(bCentered), 1);
firstGoodColumn = find(goodColumns, 1, 'first')
lastGoodColumn = find(goodColumns, 1, 'last')
% Extract (crop out) the good columns
bCentered = bCentered(:, firstGoodColumn:lastGoodColumn);
% Optional: replace nan's with some filler value, like 0 or -1 or whatever you want.
bCentered(isnan(bCentered)) = 0;

More Answers (0)

Asked:

on 28 Oct 2021

Commented:

on 31 Oct 2021

Community Treasure Hunt

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

Start Hunting!