I need help with a for loop that gives me an assignment error.

1 回表示 (過去 30 日間)
Barbara Wortham
Barbara Wortham 2018 年 5 月 7 日
コメント済み: Barbara Wortham 2018 年 5 月 7 日
Hi all, I'm a beginner to Matlab but I have this code:
clear
clc
X_nf=randi([10 50],1,10);
sigma_x=rand(1,10);
N=numel(X_nf);
V=zeros(length(X_nf),10);
for i=1:10
V(i)=X_nf+sigma_x.*randn(1,N)
end
and it is giving me this error: In an assignment A(:) = B, the number of elements in A and B must be the same. I would like it to create the variable V that has a 10x10 matrix of numbers that are varied a little bit from the variable X_nf with the random noise I am adding.
Any help would be great. Thanks!

採用された回答

sloppydisk
sloppydisk 2018 年 5 月 7 日
When you get this kind of error you want to look at the line (11 in this case) and check the sizes of the different variables. So let's look at
size(V(i))
size(X_nf)
size(sigma_x.*randn(1,N))
Now we see that V(i) is just a 1x1, while the others are 1x10. So instead we should probably write:
V(i, :) = X_nf+sigma_x.*randn(1,N)
This indexes the i-th row instead of just the i-th element.

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by