フィルターのクリア

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

1 回表示 (過去 30 日間)
when i try to run the follwing code, is shows the error as
Unable to perform assignment because the left and right sides have a different number of elements.
in the line "y(j)=x(j)+sigma.*randn(size(j));"
Help me out to rectify this..
Code:
Var=numel(x);
nMu=ceil(mu*nVar);
j=randsample(nVar,nMu);
if numel(sigma)>1
sigma = sigma(j);
end
y=x;
y(j)=x(j)+sigma.*randn(size(j));
Thanks in advance..
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 2 月 5 日
Is this complete code?
  • x is not defined?
  • mu?
  • j indexing without define it?

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

回答 (2 件)

KSSV
KSSV 2021 年 2 月 5 日
This error happens when you try to save more number of elements than initialized.
Example:
A = zeros(1,3) ;
A(1) = rand(1,2) % this will give error
In the above you need to save only one element in A(1) but you are saving two, so error pops out.
A = zeros(1,3) ;
k = []; % empty matrix
A(1) = [] % error
The above also throws error. I included this example keeping @Walter Roberson sir's comment.
Check your code and rethink on it.

Walter Roberson
Walter Roberson 2021 年 2 月 5 日
Oh wait... is it possible that x is a row vector? If so then with vector j, no matter whether j is a row vector or column vector, x(j) would be a row vector because x is a row vector. Vector being indexed by vector gives orientation the same as the source vector, not the orientation of the indexing vector.
randsample() in that form returns a column vector, so j is a column vector.
x(j)+sigma.*randn(size(j))
^^^^ ^^^^^ ^^^^^^^^^^^^^^
row scalar column
and row + column gives a 2D array . You would get an nMu x nMu output array, not a 1 x nMu output array.
y(j) = reshape(x(j),[],1) + reshape(sigma.*randn(size(j)), [], 1);
Or if you are sure that x is a row vector, then
y(j) = x(j).' + sigma.*randn(size(j));

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by