MATLAB Answers


How to implement a formula involving several elements of the same vector?

Asked by FW
on 23 Sep 2019
Latest activity Commented on by Shubham Gupta on 23 Sep 2019
Suppose we have two row vectors
Time = [ 1 2 3 4 5 6 7 8 9 10];
width= [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]; % width of peaks
There is a formula for peak resolution = (t2-t1)/(2*(w2+w1)), where t2 is element of Time(1,2) and t1 is element of Time(1,1). Similarly, w1 and w2 correspond to elements width(1,1) and width(1,2) respectively. With that one should be able to generate a row vector with 9 elements instead of individually calculating those nine values. In such cases how should we implement a formula involving several elements of the same vector?
Could someone also point out an intermediate level reference which discusses the sytax for operating on several elements of the same row or column vector to produce another vector of different dimension such as in this case? Thanks.


Sign in to comment.




2 Answers

Answer by Shubham Gupta on 23 Sep 2019
Edited by Shubham Gupta on 23 Sep 2019
 Accepted Answer

There are several way to do this, but most basic would be to use 'for' loop to get the feeling of how Matrix indexing works:
PeakResolution = zeros(1,length(Time)-1)
for i = 1:length(Time)-1
PeakResolution(i) = (Time(i+1)-Time(i))/(2*(width(i+1)+width(i)));
Once you get the feel of it, you can use functions or inline indexing to make your work easy. For e.g. to calculate difference between consecutive elements can be achieved using 'diff'. So, numerators for peak resolution can be written as:
PeakResolution_num = diff(Time); % diff function
% or
PeakResolution_num = Time(2:end)-Time(1:end-1); % inline indexing
and to take mean of consecutive numbers you can use movmean (only for matlab version above 2016a), so denominators become :
PeakResolution_den = movmean(width,[0,1])*2; % movmean function
PeakResolution_den(end) = [];
% or
PeakResolution_den = (width(2:end) + width(1:end-1)) % inline indexing
Now, you can simply use division to calculate PeakResolution:
PeakResolution = PeakResolution_num./(2*PeakResolution_den);
I hope it helps !


What was the purpose of first defining zeros for the PeakResolution?
When we know the size of the output array, we predefine the variable to prelocate memory for that memory, which helps in code to be executed little bit faster.
Using the same line of reasoning and using a 'for" loop, can we do averaging of two adjacent values such as in a new vector Average?
Yes, you should be able to achieve this easily using 'for' loop. Since, you want to skip the indeces you can define the vector used by for in a similar way :
Average = zeros(1, length(width)/2);
for i = 1:2:length(width)-1
Average((i+1)/2) = (width(i)+width(i+1))/2;
Here when I defined
i = 1:2:length(width)-1
I am skipping every even number from 1 to length(width). Also, note that I have maniputed "i" for 'Average' so that it starts from 1 and ends at length(width)/2.
Let me know if you have further questions.
Thanks. If we parse the notation i = 1:2:length(width)-1, it means index value of width vector starting from 1, with a step size of 2, and ending at length(width)-1.
This means indices corresponding to 1, 3, 5, 7, (10-1) = 1,3, 5, 7, 9.
Is this correct?
width= [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]; % Bolded and normal text as pairs to be averaged.
Yes, this should work as you have mentioned !

Sign in to comment.

Answer by madhan ravi
on 23 Sep 2019
Edited by madhan ravi
on 23 Sep 2019

peak_resolution = (Time(2:end)-Time(1:end-1))./...
(2*(Width(2:end)+Width(1:end-1))) % if understood correctly
doc colon
doc end

  1 Comment

Sign in to comment.