What do the codes in line 9 and 10 do in the program?

조회 수: 1 (최근 30일)
kjghj jmhmjh
kjghj jmhmjh 2022년 12월 3일
편집: Fifteen12 2022년 12월 3일
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

답변 (1개)

Fifteen12
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)
A = 4
B = max(n1)
B = 6
C = (n >= A) & (n <= B)
C = 1×5 logical array
0 1 1 1 0
D = C == 1
D = 1×5 logical array
0 1 1 1 0
E = find(D)
E = 1×3
2 3 4
y1(E) = x1
y1 = 1×4
0 5 5 5
It appears that setting D equal to C == 1 is redundant, as find already does that...

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by