Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

19 ビュー (過去 30 日間)
Error in mmirs1 (line 73)
Arr_LIS(:,l) = kron(arrL_a(:,l), arrL_e(:,l));
the code is
L=2;
Mx = 5;
My = [1,2,3,4,5,6]*2;
for mi=1:length(My)
M(mi) = Mx*My(mi);
angL_e = rand(1, L+1)-0.5;
angL_a = rand(1, L+1)-0.5;
arrL_a = (1/sqrt(Mx))*exp(-2*1i*pi*angL_a'*((0:Mx-1)-(Mx-1)/2))';
arrL_e = (1./sqrt(My(mi)))*exp(-2*pi*1i.*angL_e'*((0:My(mi)-1)-(My(mi)-1)/2))';
for l=1:L+1
Arr_LIS(:,l) = kron(arrL_a(:,l), arrL_e(:,l));
end
end
Unable to perform assignment because the size of the left side is 10-by-1 and the size of the right side is 20-by-1.

回答 (1 件)

Jon
Jon 2023 年 4 月 11 日
編集済み: Jon 2023 年 4 月 11 日
In the first iteration of the outer loop (mi = 1) Your variable Arr_LIS has 10 rows as
kron(arrL_a(:,l), arrL_e(:,l))
is 10 by 1, on the next iteration of the outer loop (mi = 2) ,
kron(arrL_a(:,l), arrL_e(:,l))
will have dimensions 20 by 1. You then try to assign a 20 by 1 column to an array that only has 10 rows. This is not possible and so MATLAB gives you the error Unable to perform assignment because the size of the left side is 10-by-1 and the size of the right side is 20-by-1
  2 件のコメント
Jon
Jon 2023 年 4 月 11 日
Since with each iteration of the outer loop, the number of rows to be saved changes, you have to put the results into a data type that can handle this. Deciding how to store the results depends upon how you want to use them.
Here is one approach in which you store the results in a cell array
L=2;
Mx = 5;
My = [1,2,3,4,5,6]*2;
% Make cell array to hold results
numResults = numel(My); % number of result arrays to be stored
Arr_LIS = cell(numResults,1);
% Preallocate array to hold values of M
M = zeros(numResults,1);
% Loop through elements of My storing results
for mi=1:numResults
M(mi) = Mx*My(mi);
angL_e = rand(1, L+1)-0.5;
angL_a = rand(1, L+1)-0.5;
arrL_a = (1/sqrt(Mx))*exp(-2*1i*pi*angL_a'*((0:Mx-1)-(Mx-1)/2))';
arrL_e = (1./sqrt(My(mi)))*exp(-2*pi*1i.*angL_e'*((0:My(mi)-1)-(My(mi)-1)/2))';
A = zeros(size(arrL_a,1)*size(arrL_e,1),L+1);
for l=1:L+1
A(:,l) = kron(arrL_a(:,l), arrL_e(:,l));
end
Arr_LIS{mi,1} = A;
end
% Show results (just for demonstration purposes)
Arr_LIS{1:end}

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by