Unable to perform assignment because the left and right sides have a different number of elements.

250 vues (au cours des 30 derniers jours)
I have this code and function. It cannot show the figure of the sigmult code. it says that I have an error :
Unable to perform assignment because the left and right sides have a different number of elements.
Error in sigmult (line 10)
y1((n>=min(n1))&(n<=max(n1))==1)=x1; % x1 with duration of y
Error in act_3 (line 37)
[y_mul,n_mul] = sigmult(y1,n1,y2,n2);
Your guidance is appreciated. Thanks
%Function
function [y,n] = sigmult (x1,n1,x2,n2)
% implement y(n) = x1(n) * x2 (n)
% [y,n] = sigmult (x1,n1,x2,n2)
% y = product sequence over n, which include n1 and n2
% x1 = first sequence over n1
% x2 = second sequence over n2 (n2 can be different from n1)
n = min(min(n1),min(n2)): 0.1 : max(max(n1),max(n2)); %duration of y(n)
y1 = zeros(1,length(n)); % initialization
y2 = y1;
y1((n>=min(n1))&(n<=max(n1))==1)=x1; % x1 with duration of y
y2((n>=min(n2))&(n<=max(n2))==1)=x2; % x2 with duration of y
y = y1 .* y2;
%Code
clc; clear;
%--------------------------------------------
[x,n] = stepseq(0,-10,10);
subplot (3,2,1);
stem (n,x);
axis ([-12 12 0 3]);
grid on;
xlabel ('n');
ylabel ('x(n)');
title ('Original Signals');
%----------------------------------------------
[y1,n1] = sigshift(x,n,2.5);
subplot (3,2,2);
stem (n1,y1); axis ([-12 12 0 3]); grid on;
xlabel ('n');
ylabel ('y1(n)');
title ('Shifted signal,x(n-2.5)');
%-----------------------------------------------
[y2,n2] = sigshift(x,n,-2.5);
subplot (3,2,4);
stem (n2,y2);
axis ([-12 12 0 3]);
grid on;
xlabel ('n');
ylabel ('y2(n)');
title ('Shifted signal,x(n+2.5)');
%-------------------------------------------------
[y_add,n_add] = sigadd(y1,n1,y2,n2);
subplot (3,2,6);
stem (n_add,y_add,'r');
axis ([-12 12 0 3]);
grid on;
xlabel ('n');
ylabel ('y1(n) + y2(n)');
title ('Added Signal')
%---------------------------------------------------
[y_mul,n_mul] = sigmult(y1,n1,y2,n2);
subplot (3,2,5);
stem (n_mul,y_mul,'k');
axis ([-12 12 0 3]);
grid on;
xlabel ('n');
ylabel ('y1(n) * y2(n)');
title ('Multiplied Signal');
%--------------------------------------------------

Réponses (2)

KSSV
KSSV le 1 Nov 2021
Error is simple; you are trying to save more/ less number of elements than the LHS is indexed.
% Example:
A = zeros(1,5) ;
A(1) = rand(1,2) ; % trying to save two numbers at single location; so error
A(1:3) = rand(1,2) ; % trying to save two numbers at two locations; so error
Run debugging and check it. May be you need to replce these lines:
y1((n>=min(n1))&(n<=max(n1))==1)=x1; % x1 with duration of y
y2((n>=min(n2))&(n<=max(n2))==1)=x2; % x2 with duration of y
with
idx1 = (n>=min(n1))&(n<=max(n1))==1 ;
y1((idx1)=x1(idx1); % x1 with duration of y
idx2 = (n>=min(n2))&(n<=max(n2))==1 ;
y2(idx2)=x2(idx2); % x2 with duration of y

Bjorn Gustavsson
Bjorn Gustavsson le 1 Nov 2021
It appears because you try to squeeze in/stretch out an array with some number of elements into different number of elements of another array. For you to find out why (more important to learn sooner rather than later) run your code with debugging enabled. At the command-line tell matlab to stop at errors:
dbstop if error
or for more orderly analysis:
dbstop in sigmult
Then you will get an interactive command-line in the workspace from the line where the error occured (first version) or at the first line of the sigmult-function. From there you can inspect the variables and test different commands, for the second version you can step through the function line-by-line and inspect and plot variables and test the next line to understand what goes wrong.
You will have the cause of your current problem figured out in no time, I'm sure of it!
HTH

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by