I have one vector of twenty "ideal" values, and I'm able to add normally distributed error to those values by
error = good+1*randn(size(good));
but what I want to do is generate a 20x100 matrix of these values. Specifically what I want to do is calculate a vector of another variable that depends on the value of this one, so that I get an output of 20x100 of each.
I'm trying to use a for loop to accomplish this but I'm clearly doing it wrong:
for i = 1:100;
bad(i) = good+1*randn(size(good));
newVar(i) = stuff/bad(i)
end
I'm getting the age-old 'elements of A and B must be the same'. I've tried preallocating the size of "bad", subscripting good (i.e. good(i)) in one and both places and I don't know what else to try. Why will this work for one iteration but not 100?

 Réponse acceptée

James Tursa
James Tursa le 5 Oct 2017
Modifié(e) : James Tursa le 5 Oct 2017

0 votes

One way, where the 3rd and 4th dimensions of the result are the 20x100 matrix of values for each original vector element.
err = bsxfun(@plus, good, 1*randn([size(good) 20 100]));
Or, if the 20 was the size of good and what you really meant was a "vector" of 100 for each of the 20 good values, then the following where the "vector" of results for each element of good is in the 3rd dimension:
err = bsxfun(@plus, good, 1*randn([size(good) 100]));
I changed "error" to "err" since "error" is the name of a pre-existing MATLAB function.

1 commentaire

Elizabeth Kahler
Elizabeth Kahler le 6 Oct 2017
Works perfectly, and I can even use "err" to create the second one like I wanted. Thank you, this one is new to me!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by