Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

how i can save the value of two signals when they are equal

조회 수: 1 (최근 30일)
esmaeil keyvanloo
esmaeil keyvanloo 2019년 4월 19일
마감: MATLAB Answer Bot 2021년 8월 20일
i want to save the value of these two signals when they have an equal value in each period
i wrote this code but it's not true
a = zeros(1,14);
if signal_1 == signal_2
a(b) = signal_1
b = b+1;
end
b=mod(b,14);
code needs to define the value of "b" and if i define that it cant continues to 14
  댓글 수: 2
Raj
Raj 2019년 4월 19일
Your 'if' condition logic will not serve the purpose. Depending on the first value of the signals, either the execution will not enter this condition or will enter and exit with only one match. Do you have the equations/workspace variables for signal_1 and signal_2?
esmaeil keyvanloo
esmaeil keyvanloo 2019년 4월 19일
no, signals produced by Simulink blocks
that's ok, I want to just when conditions are true the program save a new value on a(b)

답변 (3개)

Walter Roberson
Walter Roberson 2019년 4월 19일
편집: Walter Roberson 2019년 4월 19일
N = 14;
a = zeros(1, N);
b = 1;
for K = 1 : length(signal_1)
if signal_1(K) == signal_2(K)
a(b) = signal_1(K);
b = mod(b, N) + 1;
end
end
I know that mod(b,N)+1 might look wrong, but it is correct. b = 1 is sent to 2, b = 2 is sent to 3, ... b = 13 is sent to 14, b = 14 then mod(b,14) is 0 and then you add 1 to that giving 1, so b = 14 is sent to 1.
  댓글 수: 2
esmaeil keyvanloo
esmaeil keyvanloo 2019년 4월 19일
thanks
your code is true, but this is a part of program and program should be run as long as conditions of if are fulfilled
Walter Roberson
Walter Roberson 2019년 4월 19일
I have to guess about meaningful outputs of this.
function a_copy = fcn(signal_1, signal_2)
N = 14;
persistent a b
if isempty(a) || isempty(b)
a = zeros(1, N);
b = 1;
end
if signal_1 == signal_2
a(b) = signal_1;
b = mod(b,N) + 1;
end
a_copy = a;

esmaeil keyvanloo
esmaeil keyvanloo 2019년 4월 19일
thanks, but still needs to define b
Untitled.jpg
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 4월 19일
You could try adding
if isempty(b)
b = 1;
end

esmaeil keyvanloo
esmaeil keyvanloo 2019년 4월 19일
grateful
unfortunately, that has the same error and need to define the value of b.
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 4월 19일
Perhaps you could use a global block to store a and b
Or make them signals that are given initial values and which feed back into the block. Just make sure that you have a unit delay block so you do not accidentally create an algebraic loop.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by