Kvothe
Kvothe

Reputation: 15

Fill cell array with a certain value in matlab

First question: say I have a 3x3 cell array, lets call it A. So, if I want to fill A{1:2, 1:2} with the same cell array, how do I do it. MatLab requires both sides of the '=' to have the same number of elements. How do I assign the same value (a 2x1 cell) to A{1:2, 1:2}, in a single instruction?

Second question: I want to create a probability generator (not sure if it's the correct term) that will pick between a certain amount of option, based on a prior probability. For example, say that I want to randomly pick between A, B, and C, based on the following probabilities:

P(A) = .4
P(B) = .5
P(C) = .1

How do I accomplish this?

Upvotes: 0

Views: 3991

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283634

For your first question, repmat should work well.

For an example, see http://www.mathworks.com/matlabcentral/answers/8977

For your second question, combine <, cumsum, and find. If you want a more detailed explanation, open a second question covering just the probability generation.

Upvotes: 2

Related Questions