Is there an efficient way of coding other than what I did below while converting matlab code to Julia? Especially when the parent_matrix is of the size 2000X2000 and inner_matrix is of size 800X1?
Matlab:
parent_matrix(inner_matrix(:),inner_matrix(:)) = replace_matrix;
Julia:
ind_inner_vec=reshape(inner_matrix,size(inner_matrix)[1].*size(inner_matrix)[2],1)
z=1
for x in ind_inner_vec
for y in ind_inner_vec
parent_matrix[y,x]=replace_matrix[z]
z=z+1
end
end
If I understand correctly
parent_matrix[vec(inner_matrix),vec(inner_matrix)] = replace_matrix
will do what you want. Note that the vec is only needed because you said inner_matrix is a column matrix - if it was actually a vector, it wouldn't be needed.
Related
Can anybody help me with this assignment please?
I am new to matlab, and passing this year depends on this assignment, i don't have much time to explore matlab and i already wasted alot of time trying to do this assignment in my way.
I have already wrote the equations on the paper, but transfering the equations into matlab codes is really hard for me.
All i have for now is:
syms h
l = (0.75-h.^2)/(3*sqrt((5*h.^2)/4)); %h is h_max
V_default = (h.^2/2)*l;
dv = diff(V_default); %it's max. when the derivative is max.
h1 = solve( dv ==0);
h_max = (h1>0);
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
V_max = ((h_max.^2)./(2.*l_max));
but it keep give me error "Error using ./
Matrix dimensions must agree.
Error in triangle (line 9)
V_max = ((h_max.^2)./(2.*l_max)); "
Not really helping with the assignment here, but with the Matlab syntax. In the following line:
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
you're using / that is a matrix divide. You might want to use ./ which will divide the terms element by element. If I do this
l_max = (0.75-h_max.^2) ./ (3*sqrt((h_max/2).^2+(h_max.^2)));
then your code doesn't return any error. But I have no idea if it's the correct solution of your assignment, I'll leave that to you!
In line 5, the result h1 is a vector of two values but the variable itself remains symbolic, from the Symbolic Math Toolbox. MATLAB treats such variables slightly different. For that reason, the line h_max = (h1>0) doesn't really do what you expect. As I think from this point, you are interested in one value h_max, I would convert h1 to a regular MATLAB variable and change your code to the following:
h1 = double(solve( dv ==0)); % converts symbolic to regular vectors
h_max = h1(h1>0); % filters out all negative and zero values
l_max = (0.75-h_max.^2)/(3*sqrt((h_max/2).^2+(h_max.^2)));
V_max = ((h_max.^2)./(2.*l_max));
EDIT.
If you still have error, it means solve( ...) returns more than 1 positive values. In this case, as suggested, use dotted operations, such as ./ but the results in l_max and V_max will not be a single value but vectors of the same size as h_max. Which means you don't have one max Volume.
I have an mxn matrix X of return values, where I want to add a constant term c for each element of the following sub matrix Y of my original matrix X.
Y = X(end-4:end,:)
Is there a possibility avoiding a loop?
Thanks for any help!
Generate some sample data
X = rand(6,6)
X =
0.9696054 0.7389534 0.7440913 0.2781074 0.0622399 0.0154607
0.8043438 0.8845991 0.1999374 0.2341657 0.6345166 0.8774855
0.0092971 0.1108798 0.1118406 0.6249466 0.3932468 0.4050876
0.6970928 0.1084640 0.0937833 0.8243776 0.7633255 0.0650740
0.3161001 0.4452197 0.1290970 0.5837050 0.5709813 0.2331514
0.0739229 0.5626630 0.8300330 0.9590604 0.0852536 0.0225583
I do Y=X so that that X and Y will have the same dimensions and it can easily be seen where the addition occured. It's for display purposes, really.
Y=X;
Add the constant to the elements you want, element-wise
Y(end-4:end,:) = X(end-4:end,:)+4
Y =
0.969605 0.738953 0.744091 0.278107 0.062240 0.015461
4.804344 4.884599 4.199937 4.234166 4.634517 4.877485
4.009297 4.110880 4.111841 4.624947 4.393247 4.405088
4.697093 4.108464 4.093783 4.824378 4.763326 4.065074
4.316100 4.445220 4.129097 4.583705 4.570981 4.233151
4.073923 4.562663 4.830033 4.959060 4.085254 4.022558
The relevant elements are now four times larger than they originally were.
I have written a code where i have to control, if the position (x,y) (saved in the Matrix Mat) is inside of a circular object which is centered at (posx,posy). If so the point gets a value val otherwise its zero.
My Code looks like this but as a matter of fact it is advertised to NOT use loops in matlab. Since i use not 1 but 2 loops, i was wondering if there is a more effective way for solving my problem.
Mat = zeros(300); %creates my coordinate system with zeros
...
for i =lowlimitx:highlimitx %variable boundary of my object
for j=lowlimity:highlimity
helpsqrdstnc = abs(posx-i)^2 + abs(posy-j)^2; %square distance from center
if helpsqrdstnc < radius^2
Mat(i,j)= val(helpsqrdstnc);
end
end
end
the usual way to optimize matlab code is to vectorize the operations. This is because built in functions and operators is in general much faster. For your case this would leave you with this code:
Mat = zeros(300); %creates my coordinate system with zeros
...
xSq = abs(posx-(lowlimitx:highlimitx)).^2;
ySq = abs(posy-(lowlimity:highlimity)).^2;
helpsqrdstnc = bsxfun(#plus,xSq,ySq.'); %bsxfun to do [xSq(1)+ySq(1),xSq(2)+ySq(1),...; xSq(1)+ySq(2),xSq(2)+ySq(2)...; ...]
Mat(helpsqrdstnc < radius^2)= val(helpsqrdstnc(helpsqrdstnc < radius^2));
where helpsqrdstnc must be the same size as Mat. There may also be neseccary to do a reshape here, but you will notice that by yourself if you get a column vector.
This does of course assume that radius, posx and posy is constant, but reading the question this seems to be the case. However, I do not know exactly how val looks, so it I have not managed to test the code. I also think that val(helpsqrdstnc) is tedious, since this refer to the distance, which does not neseccarily need to be an integer.
In my application, I need to find the "closest" (minimum Euclidean distance vector) to an input vector, among a set of vectors (i.e. a matrix)
Therefore every single time I have to do this :
function [match_col] = find_closest_column(input_vector, vectors)
cmin = 99999999999; % current minimum distance
match_col = -1;
for col=1:width
candidate_vector = vectors(:,c); % structure of the input is not important
dist = norm(input_vector - candidate_vector);
if dist < cmin
cmin = dist;
match_col = col;
end
end
is there a built-in MATLAB function that does this kind of thing easily (with a short amount of code) for me ?
Thanks for any help !
Use pdist2. Assuming (from your code) that your vectors are columns, transposition is needed because pdist2 works with rows:
[cmin, match_col] = min(pdist2(vectors.', input_vector.' ,'euclidean'));
It can also be done with bsxfun (in this case it's easier to work directly with columns):
[cmin, match_col] = min(sum(bsxfun(#minus, vectors, input_vector).^2));
cmin = sqrt(cmin); %// to save operations, apply sqrt only to the minimizer
norm can't be directly applied to every column or row of a matrix, so you can use arrayfun:
dist = arrayfun(#(col) norm(input_vector - candidate_vector(:,col)), 1:width);
[cmin, match_col] = min(dist);
This solution was also given here.
HOWEVER, this solution is much much slower than doing a direct computation using bsxfun (as in Luis Mendo's answer), so it should be avoided. arrayfun should be used for more complex functions, where a vectorized approach is harder to get at.
I'm stumped. What is going on with MATLAB's syntax?
clear all;
dx = .1;
x=-2:dx:2;
f=zeros(length(x),1);
int_f=zeros(length(x),1);
for n=1:length(x)
f(n)=x(n).^2;
int_f(n) = f(n)*dx+int_f(n);
end
plot(x,int_f(n));
I think you should be plotting that using plot(x,int_f); that way you plot the two arrays rather than one array against one single number.
Also your integral step is wrong, it should be int_f(n) = f(n)*dx+int_f(n-1) except for the first run, where it should be int_f(n) = f(n)*dx
Because in plot(x,int_f(n));, x is a row vector, but int_f(n) is a scalar value. You should be plotting a vector against a vector.