MATLAB : Error using histc. First Input must be a real non-sparse numeric array - matlab

I've got the error of "Error using histc. First Input must be a real non-sparse numeric array" from the following codes.
N=10^4;
d=rand(1,N)>0.5;
symbols=unique(d);
probs = histc(d,symbols)./numel(d);
P/s: I try to generate using randsrc before. It did worked.But, I'm hoping not to use randsrc because it will affect my code later on. Any ideas on this would be appreciated.
Thanks.

Here is the working code
N=10^4;
d=double(rand(1,N)>0.5);
symbols=unique(d);
probs = histc(d,symbols)./numel(d);

Related

Error about C_outflow[] when using FluidPort

i'm noob for modelica and developing the Heat Exchanger model.
I did input
air_in.X_outflow[1]=1;
air_out.X_outflowp[1]=1;
coolant_in.X_outflow[1]=1;
coolant_out.X_outflow[1]=1;
but I encountered some errors like captures below.
First, I don't know what is the physical meaning of C_outflow. I could find that C_outflow = c_i/m
and m is the mass of the fluid, but couldn't find the meaning of c_i.
Second, I've just tried to input the value 0~1 as the description said, but encountered the error message above. I think the value has not been input to the C_outflow array but I'm not sure.
Please reply to anyone who can figure out these problems... Thank you to all of you.
model Staggered_HX
A complete model would be helpful - but attempting to answer anyway:
The C-array is an array of trace substances, and there are normally no trace substances; whereas the X-array is the array of normal substances - which must contain some elements.
For the C-array it would thus be: air_in.C_outflow=zeros(0); (where zeros(0) creates an empty vector - corresponding to no trace substabces), but I don't see how that relates to the equations above with air_in.X_outflow[1]=1;

What does [val... ].^2 mean?

I'm porting matlab code to python and come across the code below. Looks like it creates a matrix but I'm not sure what the shape of the matrix would be. Can anybody help me understand what this code mean especially '...' and '].^2'?
somevarialbe = [var1...
var2...
var3].^2;
It is the item-wise operator and power each item to 2. In the other words, it is equivalent to the following code:
somevarialbe = [var1^2...
var2^2...
var3^2];
And ... means next line in the code. Hence, it is equivalent to the following code:
somevarialbe = [var1^2 var2^2 var3^2];

How can I run createOptimProblem in matlab?

My question is a little bit stupid, but still, I would like someone who can help me if he/she face the same problem as me.
I copy the codes directly from MATLAB mathwork:
anonrosen = #(x)(100*(x(2) - x(1)^2)^2 + (1-x(1))^2);
opts = optimoptions(#fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',randn(2,1),...
'objective',anonrosen,'lb',[-2;-2],'ub',[2;2],'options',opts);
However, it gives the following error message:
Undefined function 'createOptimProblem' for input arguments of type 'optim.options.Fmincon'.
createOptimProblem should be a built-in function, but with the presence of the error message, I wonder if I need to declare sth before using createOptimProblem, what should I do?
I am using R2013a version

Where is my mistake and how can i correct it?

I'm using this code for parameter estimation. it gives me an error in line given below. how can i correct this. thanks in advance.
while dcnorm>1E-6 & iter<10
f=a*(b.^(c.^t))-y;
Ji1=b.^(c.^t);
Ji2=a*(b^(c.^(t-1)))*(c.^t); %ERROR LINE
Ji3=a*(b^(c.^t))*ln(b)*t*(c.^(t-1));
J=[Ji1 Ji2 Ji3];
dc=-J\f; tahmin=tahmin+dc;
dcnorm=norm(dc); iter=iter+1;
a=tahmin(1); b=tahmin(2); c=tahmin(3);
D=[iter a b c norm(f) norm(dc)]
end
try (explicitly use element-wise operations throughout the expression):
Ji2=a.*(b.^(c.^(t-1))).*(c.^t);
My guess you'll need to modify the next line as well.

Debugging a for loop in matlab

I've been looking throught the documentation, but can't seem to find the bit I want.
I have a for loop and I would like to be able to view every value in the for loop.
for example here is a part of my code:
for d = 1 : nb
%for loop performs blade by blade averaging and produces a column vector
for cc = navg : length(atbmat);
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
atbvec2(:,cc) = atb2;
end
%assigns column vector 'atbvec2' to the correct column of the matrix 'atbmat2'
atbmat2(d,1:length(atbvec2)) = atbvec2;
end
I would like to view every value of atb2. I'm a python user(new to MATLAB) and would normally use a simple print statement to find this.
I'm sure there is a way to do it, but I can't quite find how.
Thankyou in advance.
you can use disp in Matlab to print to the screen but you might want to use sprintf first to format it nicely. However for debugging you're better off using a break point and then inspect the variable in the workspace browser graphically. To me, this is one of Matlab's best features.
Have a look at the "Examine Values" section of this article
The simplest way to view it everywhere is to change this line:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg;
Into this, without semicolon:
atb2 = (sum(atbmat((cc-(navg-1):cc),d)))/navg
That being said, given the nature of your calculation, you could get the information you need as well by simply storing every value of abt2 and observing them afterwards. This may be done in atbmat2 already?
If you want to look at each value at the time it happens, consider setting a breakpoint or conditional breakpoint after the line where abt2 is assigned.