0

I have got two matrices, 1st one has probability in it and second one has values of power corresponding to each probability and both have 1 row and 100 columns. Now i want to generate 10000 random numbers between 0-1 which are compared to the probability and if satisfy a certain condition, the output from corresponding power matrix should be given. I have written the code but am getting an error "matrix dimension must agree". Can any on let me know hat mistake i am making here.

a=rand(1,10000);
for q=1:1:99
    praq=pr(1:1:99);
    pwaq=pw(1:1:99);
end
if a<praq
    pwaq
else if a>=praq and a<praq+1
        pwaq+1;
    end 
end
exit

Where pra is the probability, pwa is the power. I want to check if random number i.e. a is less than pra's first element, it should give output from pwa's 1st element. if not, it should check for second element of pra and so on till 100th element. This procedure should be repeated 10000 times.

zafar
  • 1
  • 1
  • Try adding a period for the types. Often that helps: `praq=pr.(1:1:99)` – cdomination Jun 23 '16 at 17:40
  • @ChristinaDocenko Are you sure that's valid MATLAB syntax? What is it supposed to do? – beaker Jun 23 '16 at 18:05
  • I usually got that error when multiplying things of different lengths. So, say a * b would give me the error, the way to correct it was a.*b... I'm not exactly sure what it does, but it helped me in that instance. – cdomination Jun 23 '16 at 18:10
  • @ChristinaDocenko Okay, `a .* b` is changing vector/matrix multiplication to element-wise multiplication. There's no operator here, just indexing an array. – beaker Jun 23 '16 at 18:11
  • Sorry, just thought it might help – cdomination Jun 23 '16 at 18:11

1 Answers1

0

You have a couple problems here:

for q=1:1:99 praq=pr(1:1:99); pwaq=pw(1:1:99); end

This loop is doing nothing, you are reassigning the same values to praq and pwaq over and over again.

if a<praq

This statement only works if a and praq are scalars, but a is a vector with length 10,000 and praq is a vector with length 99.

You need to do something like

for rand_idx = 1:length(a) if a(rand_idx) < praq(<something>) ... end

However, to accomplish your goal, rather than using if x < y I'd recommend looking at the find and isempty functions.

Trogdor
  • 1,346
  • 9
  • 16