What do the codes in line 9 and 10 do in the program?
1 次查看(过去 30 天)
显示 更早的评论
clc; close all; clear all;
n1=0:4;
x1=[0 1 2 3 4]; %given first sequence
n2=-2:2;
x2=[2 2 2 2 2]; %given second sequence
n=min(min(n1),min(n2)):max(max(n1),max(n2));
y1=zeros(1,length(n));
y2=y1;
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % What does this do?
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2; % What does this do?
y=y1+y2; %addition of the two given sequences
subplot(1,1,1);
stem(n,y);
title('Addition of the two given sequences.');
Above is a program given by my professor to perform addition of two Discrete signals in MATLAB. I've understood what every line of the program does except the two lines shown below
y1(find((n>=min(n1))&(n<=max(n1))==1))=x1; % What does this do?
y2(find((n>=min(n2))&(n<=max(n2))==1))=x2;
Can someone please tell me what exactly do these lines do in the program and what is the logic of it?
Thank you
0 个评论
回答(1 个)
Fifteen12
2022-12-3
编辑:Fifteen12
2022-12-3
This is using logical indexing to filter out specific numbers. This code is selecting all the indices in y1 that correspond to the same indices in n where n not within the range of n1 (<= to n1's minimum and >= n1's maximum). Then it's setting all these indices equal to the values in x1. Broken down step by step you get:
n = 3:7;
n1 = [4, 5, 6];
x1 = 5;
A = min(n1)
B = max(n1)
C = (n >= A) & (n <= B)
D = C == 1
E = find(D)
y1(E) = x1
It appears that setting D equal to C == 1 is redundant, as find already does that...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!