0

Our sample set is given as {-1,1.5,2,2.5}.

b = from "outcome" value to outcome(index100) value.

There is a problem that outcome(1) cannot be equal to a matrix value. Hence I set "outcome" (without (1)index) equal to the matrix value. But unfortunately, yet > or < operators cannot work with a matrix value, And I don't know how to work with rand function. In fact, I like when a negative value is randomly chosen then the next time double of it being selected, but when a positive random value is chosen it don't get doubled. I give you the code:

sample={-1,1.5,2,2.5};
for i=1:100
    z=i;
    outcome=randsample(sample,1);
    if outcome>0
        z=1;
    else
        z=i;
        outcome(z)=(2^z)*outcome;
    end;
    if i==1
        result=outcome;
    end;
end;
b = outcome:outcome(100);
result = sum(b,"all");
plot result;
end

Any vision for helping me?

7
  • Before trying to solve the issue I'd recommend that you fix the indentation and the variable naming of your code. Looking at the code I have no idea what a, b and z variables are supposed to do. Use descriptive names for your variables. It would also be a good idea that for a smaller range (eg: for i=1:5) you provide what is the result you expect, so we can better understand what are you trying to do. Commented Oct 23, 2024 at 14:06
  • Error: Operator '>' is not supported for operands of type 'cell'. Commented Oct 23, 2024 at 14:27
  • 1
    Why are you using a cell anyway? They're best suited for non-numeric or non-uniform data... You probably want to define a numeric array sample=[-1,1.5,2,2.5]; with square brackets Commented Oct 23, 2024 at 15:00
  • In MATLAB operators < and > CAN and DO indeed operate matrixes . Try : [1 2 3] > [4 5 6] Result : 1×3 logical array 0 0 0 Commented Oct 23, 2024 at 15:16
  • You intend for result to be a single value, sum of all outcome? Because outcome:outcome(100) is an array with all integer values in between outcome(1) and outcome(100), if both of them are integers, the other values in outcome would be ignored. I think you meant to do outcome(1:100). What is plot result meant to do? outcome(i)=randsample(sample,1); would work if sample is defined as a numeric array as @wolfie suggested. Commented Oct 23, 2024 at 15:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.