can't get ny script running. What could be the problem?

1 view (last 30 days)
a=4:-1:4;
w=a*randn(350,1);
n = 400000;
proc_1= zeros(1, n);
for i = 1 : n
if rand() <= 0.5
proc_1(k) = x1(randi(length(x1)));
else
proc_1(k) = x2(randi(length(x2)));
end
resultant(k)=proc_1(k)+w(k);
end
i'm running this script but it keeps giving me this error=> " ??? Index exceeds matrix dimensions."
  2 Comments
Optics Wizard
Optics Wizard on 9 Apr 2020
^I concur. OP, please post the entire code. Or, maybe the answer is that you haven't defined x1 or x2...

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 9 Apr 2020
w=a*randn(350,1);
so w is length 350
n = 400000;
for i = 1 : n
so i will go to 400000
resultant(k)=proc_1(k)+w(k);
k is not defined in anything we see, but it could easily exceed 350. Especially if k is intended to be the same as i, in which case proc_1(k) would be valid up to k = 400000 but w(k) is only valid up to k = 350
  3 Comments
Steven Lord
Steven Lord on 10 Apr 2020
You can't retrieve the 400000th element of a vector if the vector doesn't have a 400000th element. You're asking for something that simply does not exist.
Walter Roberson
Walter Roberson on 10 Apr 2020
You could "reuse" w values, such as
w(mod(k-1,350)+1)
which would use w in cycles of 350 (that is, k=351 and 701 and 151 would all map to w(1))
But it is not at all clear that that would be desirable.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!