How do I delete a repeated element in a single array cell?

2 Ansichten (letzte 30 Tage)
Goncalo Costa
Goncalo Costa am 17 Jan. 2023
Kommentiert: dpb am 18 Jan. 2023
I am trying to applying a filter through an array of signals, but the filter must be aligned with the signal's peak (max value). For this I wrote the following:
desv = 10; %constant for the filter function below
filt = @(x) exp(-(t-t(x)).^2/desv); %filter function depending on t (a time array)
%to select the x for the correct time point, I must find the max of each
%signal
b2 = {};
samn = {};
for x = 1:size(sam_pulse,1) %sam_pulse is the 2295 x 449 data array
[~,b3] = find(sam_pulse(x,:) == max(sam_pulse(x,:))); %find the cell with max value
b2{x} = b3; %store it in the empty cell above
filt_b2 = filt(b2(x)); %use function on the discovered time point
samn(x) = sam_pulse.*filt_b2; %apply the filter on each row of the data array
end
The problem is that some of the signals in the array have more than one cell with the same max value. This means that, for example, b2{17} = 174 and 175.
I must apply the filter to a single point, so I want to ignore the second max value, whilst still keeping it in its array. By this, I mean that I want the max value to be identified on cell 174. And therefore align my filter with said cell 174.
How can I tell the programme to solely store the first number on b2?

Akzeptierte Antwort

dpb
dpb am 17 Jan. 2023
Bearbeitet: dpb am 17 Jan. 2023
In
[~,b3] = find(sam_pulse(x,:) == max(sam_pulse(x,:))); %find the cell with max value
you're doing the wrong thing using max(); see the <max> docs and look at the optional second output for it...
...
[~,imx]=max(sam_pulse(x,:)); % returns first location of max in vector
b2{x}=imx;
filt_b2 = filt(b2(x)); %use function on the discovered time point
...
Then you'll always have only one value for imx (the first location in the vector).
If it isn't the first wanted, then you would need to define which one would be the one wanted and in that case, if it were the last, then run max over the reversed vector. If it were something other than the first or last in the case of more than two, then reverting to the use of find and selecting the Nth would be necessary as well as the logic to determine how many there really were.
You could fix the problem with a klunky kludge, of course, by just using
b2{x} = b3(1); %store it in the empty cell above
to always pick the first from the present code although I don't recommend solving it that way; as a general rule, don't use find unless it's really needed, and it isn't here since using max will already do the deed for you without the explicit call.
  2 Kommentare
Goncalo Costa
Goncalo Costa am 18 Jan. 2023
Thank you so much for this solution. This is really helpful, and awfully simple somehow. Thank you.
There is just one thing I am curious about, that first statement regarding my incorrect use of max. I have done what you have told me, to "look at the optional second output " of max in the documentation. But I have found nothing besides how to obtain the max values in array, and not their indices. Was there something that I should have looked deeper into?
Thank you once again!
dpb
dpb am 18 Jan. 2023
Ayup...look at
>> help max
max Maximum elements of an array.
M = max(X) is the largest element in the vector X. If X is a matrix, M
is a row vector containing the maximum element from each column. For
N-D arrays, max(X) operates along the first non-singleton dimension.
...
[M,I] = max(X) also returns the indices into operating dimension
corresponding to the maximum values. If X contains more than one
element with the maximum value, then the index of the first one
is returned.
...
See the second output syntax?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by