I noticed what was for me an unpredicted behavior when evaluating vectors. It seems is really different doing it directly than indexing within a loop. Can anyone give me some help with this?
i know is probably explained in how it makes each operation, so i need some keys about how to look for it
thanks for advise
thanks in advance
example:
x=[0.05:.01:3];
n=size(x,2);
y1 = (8*x-1)/(x)-(exp(x));
for i=1:n
y2(i)=(8*x(i)-1)/(x(i))-(exp(x(i)));
end
a1=[x;y1];
a2=[x;y2];
plot(x,y1, 'red')
hold on
plot(x,y2, 'blue')
here the plot:
http://i.stack.imgur.com/qAHD6.jpg
results:
a2:
0.05 -13.0513
0.06 -9.7285
0.07 -7.3582
0.08 -5.5833
0.09 -4.2053
0.10 -3.1052
0.11 -2.2072
0.12 -1.4608
0.13 -0.8311
0.14 -0.2931
0.15 0.1715
0.16 0.5765
a1:
0.05 6.4497
0.06 6.4391
0.07 6.4284
0.08 6.4177
0.09 6.4068
0.10 6.3958
0.11 6.3847
0.12 6.3734
0.13 6.3621
0.14 6.3507
0.15 6.3391
0.16 6.3274
What you want is:
y1 = (8*x-1)./(x)-(exp(x)); % note the ./
instead of:
y1 = (8*x-1)/(x)-(exp(x));
As a side note, you can type help / to see what your original first statement was really doing. It was effectively doing (x'\(8*x-1)')' (note the backslash).
The error is in your first vector calculation of y1. The problem is in the part where you divide by x. Vector/Matrix division is different from single element division and will return a vector/matrix result.
The solution is to do element-by-element division of these vectors. The vector notation shortcut for this is to use ./ (dot-divide):
y3 = (8*x-1)./(x)-(exp(x));
Now you should find that y2 and y3 are identical.
Related
If I've got a list of values x, what's the easiest way to make a histogram with bin-size b?
It seems like I could hack a complicated solution, but I feel like there must be a built-in function for this somewhere that I don't know about.
I haven't heard about built-in histogram so far. But I would approach this task like below.
For fixed bucket size:
a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: 0.1;
{count each group x xbar y}[b;a]
// returns 0.3 0.5 0.4 0.1 0.7!2 3 2 1 2j
For "floating" buckets:
a: 0.39 0.51 0.51 0.4 0.17 0.3 0.78 0.53 0.71 0.41;
b: -1 0.5 0.7 1;
{count each group x#x bin y}[b;a]
// returns -1 0.5 0.7!5 3 2j
Above functions return dictionary with bucket starts as keys and number of bucket occurrences as values.
Assuming you have a list of x values (let's assume x = 1000):
v:1000?1.0;
You can achieve what you need as follows:
b:0.1;
hist:(count') group xbar[b;v];
There are two points:
the keys in hist are not sorted
For the bucket, do you prefer to output the left or the right delimiter?
To solve for 1), you simply do:
hist:(asc key hist)#hist;
To solve for 2) - I mean, if you want to have the right delimiter:
hist:(+[b;key hist])!value hist;
I have an optimization problem.
My objective function is a linear function which is:
f=h(1)+h(2)+h(3)+h(4)+h(5)
where h(i)=t(i+1)-t(i)
My constraints are non-linear:
v_{ji}-v_{max,j}<=0 and a_{ji}-a_{max,j}<=0
where
for m=1:6
for i=2:4
v_{ji}=abs(((ddq(m,i+1)*(t(i+1)-time(j))^2)/(2*h(i)))+((ddq(m,(i+3))*(time(j)-t(i))^2)/h(i))+((path(m,i+1)/h(i))-(h(i)*ddq(m,(i+3)))/6)-((path(m,i)/h(i))-(h(i)*(ddq(m,i+1))/6)))-v_{max,j};
end
end
and
for m=1:6
for i=2:4
a_{ji}=abs(((ddq(m,(i+2))/h(i))*(time(j)-t(i)))-((ddq(m,i+1)/h(i))*(time(j)-t(i+1))))-a_max(j);
end
end
time(j) is a random value between t(i) and t(i+1). I used fmincon function to obtain the optimized h values where I set the options as:
options=optimset('Algorithm','sqp','Display','iter','DiffMinChange',1e-6,'DiffMaxChange',1e-1,'TolFun',1e-10,'TolX',1e-16,'MaxFunEvals',10000,'MaxIter',250);
When I run the code I got different results for different initial values such as:
for h_0=[1 1 1 1 1], h=[0.052 0.087 0.104 0.007 0.002]
for h_0=[0.5 0.5 0.5 0.5 0.5], h=[0.020 0.059 0.106 0.011 0.003]
for h_0=[0.1 0.1 0.1 0.1 0.1], h=[0.017 0.05 0.107 0.017 0.042]
I also tried to solve the same problem with active-set and interior-point algorithms and the values obtained from active set is
for h_0=[0.1 0.1 0.1 0.1 0.1], h=[0.094 0.106 0.1 0.1 0.1]
and it didn't work for different values.
interior-point algorithm also didn't work and I couldn't get any results.
My questions are:
1. Why did I get different results for each different initial value?
2. Why didn't other algorithms work for this question?
3. If I had to change algorithm which one is the best for this problem and why?
I have two tables of different size. I'm using Matlab, it doesn't matter I just need the logic of doing this
the first table contains 8 elements the second one contains 3
0.04 0.1
0.08 0.2
0.12 0.3
0.16
0.20
0.24
0.28
0.32
I want to compare the two tables, repeating same value in the second table while tab1(i) < tab2(i) in order to get two tables of the same size the result must be like this
0.04 0.1
0.08 0.1
0.12 0.2
0.16 0.2
0.20 0.2
0.24 0.3
0.28 0.3
0.32 0.0
i've tried this
for ii=1:sizex1(1)
for jj=1:ssimagefile
if x2imagefile(jj)<=z1(ii)
tab2(ii)=z1(ii)
fprintf(file, '%f %f \n', [ii tab2]');
jj=jj+1;
else
ii=ii+1;
end
end
Here is a Matlaby way to do it:
%// Add zeros to the end of tab2 so that it is as long as tab1
tab3(numel(tab1)) = 0;
%// use bsxfun to find for each number, which rows it will be replicated into
I = bsxfun(#le, tab1, tab2') & bsxfun(#gt, tab1, [-inf,tab2(1:end-1)']);
%// use find to convert the rows from the step before to indexes of where the numbers lie in tab1
[~,c] = find(I);
%// populate tab3 with the repeated numbers from tab2
tab3(1:numel(c)) = tab2(c);
And a simpler way using for loops:
tab3 = zeros(size(tab1));
for row = 1:numel(tab1)
idx = tab2 > tab1(row);
if any(idx)
tab3(row) = min(tab2(idx));
end
end
You could also take the following vectorized approach in case you prefer to avoid bsxfun:
tab2_sorted = sort(tab2); % Sort tab2
tab3=zeros(size(tab1)); % Initialize the new table
% Fill the new table with the values of tab2
tab3(tab1<=tab2_sorted(3))=tab2_sorted(3);
tab3(tab1<=tab2_sorted(2))=tab2_sorted(2);
tab3(tab1<=tab2_sorted(1))=tab2_sorted(1);
I have a random generated matrix
A =[ 0.7015 -1.577 -1.333 0.022 -0.5 -2.00 -0.034 -0.714
-2.05 -0.5 1.12 -0.26 -0.97 0.96 -0.79 1.35
-0.353 0.28 -0.5 -1.75 -1.15 0.52 1.018 -0.22
-0.8 0.033 -0.29 -0.28 -0.5 -0.02 -0.13 -0.58 ]
I want to find the common values of all rows.Each row has no duplicated elements. Can anyone give me a help?
Get a vector of unique values with unique, and then compare each element of A with each unique value using bsxfun:
u = unique(A);
m = squeeze(all(any(bsxfun(#eq, A, permute(u, [2 3 1])),2),1));
result = u(m);
This should be fast, but may be memory-hungry, as it generates a 3D array of size mxnxp, where A is mxn and p is the number of unique values of A. It works even if a row can contains duplicated elements.
Exploiting the fact that each row has no duplicated elements, you can use a possibly more memory-eficient approach with accumarray:
[u, ~, w] = unique(A);
m = accumarray(w,1)==size(A,1);
result = u(m);
**updated
I want treat float or double only with 2 digit in float section in matlab.as example
a=23.1234443434343434454545444;
I want if I use a it is equal
a=23.12;
any idea in matlab?
I want funcion like round(1.95583, 2);//return 1.95 (php function ) in matlab
If you want to display values with only 2 digits, you can do
>> format bank
>> pi
ans =
3.14
>> rand(4)
ans =
0.81 0.63 0.96 0.96
0.91 0.10 0.96 0.49
0.13 0.28 0.16 0.80
0.91 0.55 0.97 0.14
This does NOT mean calculations will be carried out with less precision.
If the latter is really what you want (why?!), you could use constructions like
ppi = single(pi)
for reduced precision, or
f = #(x) double(uint64(x*100))/100;
for guaranteed 2-digit precision. In the last case, you have to pass all values through the function f prior to using them:
>> ppi = f(pi)
ans =
3.140000000000000
>> f(rand(4))
ans =
0.280000000000000 0.690000000000000 0.440000000000000 0.190000000000000
0.050000000000000 0.320000000000000 0.380000000000000 0.490000000000000
0.100000000000000 0.950000000000000 0.770000000000000 0.450000000000000
0.820000000000000 0.030000000000000 0.800000000000000 0.650000000000000
If you're looking for a more elegant solution for this last case, use the fixed-point toolbox, as Danil suggested.
If you want to just keep N digits, another formulation is:
>> d = 10^(-2)
>> round(pi/d)*d
ans =
3.1400
Edit:
As per Rody's comment, round might not truncate properly, so use:
>> a = 3.146
>> fix(a/d)*d
ans =
3.1400