# Extract Numerator and Denominator of a vector (ratio)

2 views (last 30 days)
Maria Amr on 13 Jun 2021
Commented: Maria Amr on 14 Jun 2021
Hello Everyone,
I appreciate your helps in advance as I am a beginner in MATLAB!
I have a matrix and I should measure a ratio of a simple variable (it is a signal) and I need separate numerators and denominators (separated vectors) for next steps. My code is below. Thank you!
clc;
clear all;
close all;
%.....................................................................
amp=data(:,2);%us/f
time=data(:,1);%ms
t=time(1:1:end);% distans approximately 1 ms
am=amp(1:1:end);
l=length(t);
for i=1:l
if am(i)==-999.25
am(i)=am(i-1);
end
end
for j=2:l
r(j)=(am(j)-am(j-1))/(am(j)+am(j-1));
end

Scott MacKenzie on 13 Jun 2021
Edited: Scott MacKenzie on 13 Jun 2021
It is not entirely clear what you are trying to do. Your 1st for-loop does nothing, since all the values are positive. However, if I understand correctly, your second for-loop reveals how you want to spit the signal into terms for the numerator (us) and denominator (f) terms. If that's correct, then, instead of a single vector r, you likely want separate vectors, us and f. Given this, here is what I put together:
amp = data(:,2); % signal (us/f)
time = data(:,1); % time (1 ms intervals, not needed)
% this is equivalent to your second for-loop (except separating the terms)
us = amp(2:end) - amp(1:end-1);
f = amp(2:end) + amp(1:end-1);
tiledlayout(2,1);
nexttile;
plot(amp);
xlabel('Time (ms)');
ylabel('Signal');
nexttile;
p1 = plot(f);
hold on;
p2 = plot(us);
xlabel('Time (ms)');
ylabel('Signal (separated)');
legend({'f' 'us'}, 'location', 'east');
Maria Amr on 14 Jun 2021
Yes sir. You are totally right. This is aeismic data (a layered earth) and is not original data. Now, I understood how I should use the output of the us and f. Your explanation is so useful! Appreciated!