Using a signal to create a variable
4 views (last 30 days)
Show older comments
Hi!
I have three variables, for example;
A = (-1 1 0.5 0.2 -0.7)
B = (1.05 1.05 1.05 1.05 1.03)
C = (0.99 1.01 0.76 1.89 1.23)
And I want to use A as a signal for which values from B and C to create variable D. So that D = B if A<0 and D = C if A>0.
I would then get
D= (1.05 1.01 0.76 1.89 1.03)
I have tried to ask a few questions about how to code this before, but I guess I haven't been specific enough because I haven't gotten it to work. But I haven't been able to work it out on my own with the Help function. So I will give it another go. Any help woul be greatly appreciated!
0 Comments
Answers (2)
Azzi Abdelmalek
on 19 Mar 2013
A = [-1 1 0.5 0.2 -0.7]
B = [1.05 1.05 1.05 1.05 1.03]
C = [0.99 1.01 0.76 1.89 1.23]
D=C
idx=A<0
D(idx)=B(idx)
0 Comments
Image Analyst
on 19 Mar 2013
% Setup:
A = [-1 1 0.5 0.2 -0.7];
B = [1.05 1.05 1.05 1.05 1.03];
C = [0.99 1.01 0.76 1.89 1.23];
% D = B if A<0 and D = C if A>0.
% Answer: D= [1.05 1.01 0.76 1.89 1.03]
% How to do it:
useB = A<0;
D=C; % Initialize
D(useB) = B(useB)
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!