Trying to fit multiple characters into one element?

Trying to have the program output Monte Carlo (MC) mean cost, standard deviation, and CRM. Receiving an error on line 21. I think it's because I'm trying to fit multiple characters into one element. The specific error is:
Unable to perform assignment because the left and right sides have a different number of elements.
Code is shown below. I'm a Matlab newbie, so any help would be greatly appreciated!
Ns = 100; %Number of samples
Names = {'So','N1','N2','N3','N4','N5','Si'} %Specify Nodes
CTb = [60 30 10 10 50 20 70 10 20 20 10 20 50 30]; %Expected cost per TB
SDupper = CTb + CTb*0.5
SDlower = CTb - CTb*0.5
SDctb = .50;%Subjective SD for cost per TB
COSTrn = nan(1,Ns); %An array to hold random costs
COSTcrm = nan(1,Ns);%An array to hold the CRM
Sample = 1:Ns;
Nb = 10;
COSTrn(1) = (CTb + CTb*SDctb*randn()); % error - multiple characters into one element?
COSTcrm(1) = COSTrn(1);
for i = 2:Ns
COSTrn(i) = (CTb + CTb*SDctb*randn());
COSTcrm(i) = ((i-1)*COSTcrm(i-1) + COSTrn(i))/i;
end
COSTmean = mean(COSTrn) %what is the mean of all
CRM = COSTcrm(Ns) %what is the final mean
SD = std(COSTrn)
SE = SD/sqrt(Ns)
figure(1)
histogram(COSTrn,Nb)
title('Histogram of Project Durations')
ylabel('Count')
figure(2)
plot(Sample,COSTcrm)
title('CRM')
xlabel('time')
ylabel('Cumulative Percent')
axis([0,Sample(Ns),0,CTb*1.3])

3 Comments

Ctb is a vector of length 14. You do a computation with the entire vector (mentioned twice) so the right hand side is a vector of length 14. You then try to store that vector into a single element on the left.
If you fully intend to work vector by vector then you have two reasonable choices:
1. Store into a row of a 2d array. This is a good choice if all the rows will be the same size (though using columns is more efficient)
2. Use cell arrays. This is a good choice if the rows will not all be the same size.
Many thanks for the quick response! The output row will be the same size, so will try storing in a 2d array. Thank you again, Walter!

Sign in to comment.

Answers (0)

Products

Release

R2019b

Asked:

on 21 Feb 2020

Edited:

on 21 Feb 2020

Community Treasure Hunt

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

Start Hunting!