https://in.mathworks.com/matlabcentral/answers/questions.atomMATLAB Answers — New Questions2018-12-14T11:20:44Ztag:in.mathworks.com,2005:Question/4355682018-12-13T14:32:18Z2018-12-14T11:20:44ZLevenshtein distance: String scanningI'm in desperate need of help with creating a Levenshtein distance function with scanning behavior.
Basically there is a target string (s) which is supposed to scan a longer experimental string (s2). For example a target string (s) of length m = 3 should be compared to the first 3 entries of an experimental string (s2). Afterwards the target string (s) should be "moved" by 1 step to be compared to the entries 2-4 of the experimental string (s2) and so on.
Creating such a scanning behavior is not a problem, the real problems are insertions and deletions. After an insertion or deletion I still want to compare 2 strings of equal length. Therefore when inserting another entry, the last entry of the experimental string (s2) has to be removed, and when deleting an entry the next value of the experimental string should be added. Simply spoken the string lengths (m and n) should always be constant and have the same length.
Here a simple example:
For the strings s = [1 2 3] and s2 = [1 3 2 3 ...] I want to compare parts of the same length m = 3, therefore the first comparison should be between s = [1 2 3] and s2 = [1 3 2]. A deletion at position #2 should now change s2 to [entry1 entry3 entry4], in this case to s2 = [1 2 3], leading to an edit distance of 1 (with a deletion cost of 1). Here, an insertion of the value 2 at position #2 should lead to s2 = [entry1 insertion entry2] and therefore also s2 = [1 2 3].
Any ideas how I could achieve this?
I'm using an Levenshtein function similar to this:
% Variables
s = [1 2 3];
m=length(s);
s2 = [1 3 2 3 1 2 3];
t = s2(1:1:m);
n=length(t);
% Edit Matrix
mat=zeros(m+1,n+1);
for i=1:1:m
mat(i+1,1)=i;
end
for j=1:1:n
mat(1,j+1)=j;
end
for i=1:m
for j=1:n
if (s(i) == t(j))
mat(i+1,j+1)=mat(i,j);
else
mat(i+1,j+1)= min([mat(i+1,j)+1,mat(i,j+1)+1,mat(i,j)+1]);
end
end
end
%Edit distance
d = mat(m+1,n+1);Marcel Dorerhttps://in.mathworks.com/matlabcentral/profile/authors/10410471-marcel-dorertag:in.mathworks.com,2005:Question/4357272018-12-14T10:21:15Z2018-12-14T11:20:44ZHow to add GUI for existing codeHi everyone,
I have a written code of vibration shape mode. Since there are too many curves in my plot, I'm thinking about using popup menu to organize them. However, I don't know how to make it work after I created my GUI. I've tried to put my entire code into the callback of popupmenu function but it didn't work. Here's how I'm plotting it right now. I want to make the corresponding plot everytime I pick a k value from the popup menu. Can someone help me with it?
%Assign each interior displacement to its location
figure(1)
for k=1:s_e%looping over frequencies
for i=1:n%looping over elements
%Plot 14.4c elemental displacement
plot(l(i,:),vd{k}(i,:),'-x')
hold on
grid on
plot(l_b,vd_b,'-ko')%Original beam plot
end
title('Vibration mode shape with 4 elements')
%Show max displacement point for each graph
MaxVd{k}=max(max(vd{k}));
indexofMax=find(vd{k}==MaxVd{k},1,'first');
maxY=vd{k}(indexofMax);
maxX=l(indexofMax);
plot(maxX,maxY,'-k^');
text(maxX,maxY,['(' num2str(maxX) ',' num2str(maxY) ')'],'HorizontalAlignment','right','FontSize',7);
end
hold off
Yihan Chenhttps://in.mathworks.com/matlabcentral/profile/authors/6096000-yihan-chentag:in.mathworks.com,2005:Question/4352442018-12-11T22:50:03Z2018-12-14T11:20:26ZMaking My Own Simpson's Rule FunctionCan someone make me a simpsons rule functionJack Bushhttps://in.mathworks.com/matlabcentral/profile/authors/13883834-jack-bushtag:in.mathworks.com,2005:Question/4262842018-10-26T14:31:37Z2018-12-14T11:20:22ZHow can I voxelize a medium?Hello I wrote a code to draw lines in a 3D space. there are a lot of lines in different positions. I want to divide this space to find which line goes through which divsion. or find how many lines go through each division. for example lines are in space with dimension of -10 to 10 mm in x,y and z directions. and every division of space which we call voxel is a cube with 0.1 mm of each dinension. could you help me to write a code to voxelize the space and find the number of lines go through each voxel please?Merisa Saberhttps://in.mathworks.com/matlabcentral/profile/authors/11386647-merisa-sabertag:in.mathworks.com,2005:Question/1712772015-01-23T09:22:43Z2018-12-14T11:20:09ZHow can i get all paths between two nodes ??How can i get all paths between two nodes shaifali shakyahttps://in.mathworks.com/matlabcentral/profile/authors/6094891-shaifali-shakyatag:in.mathworks.com,2005:Question/4357282018-12-14T10:23:37Z2018-12-14T11:19:55ZFailure in initial nonlinear constraint function evaluation. FMINCON cannot continue.Hey Guys,
My goal is to optimize the EoM that I implied in the constraint file, so that the 5th EoM (dmdt) gets as low as possible. That means that the Mass of the rocket arriving in the final orbit is as high as possible.
I get the following Errors:
Error in constraint (line 3)
y=Y(1);
Error in fmincon (line 633)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Error in main (line 59)
[YSol,fval]=fmincon(obj,Y0,A,b,Aeq,beq,lb,ub,nonlincon);
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
I was thinking that the problem may be that I only have to pass the initial value m0 to the fmincon function instead of, as I did now, the vector
Y0=[y0;v0;h0;0;m0;w0;a0];
Firstly I get exact the same errors and secondly I do not know how to pass my other initial values to the Optimization Toolbox?
Can someone explain the errors?
Thanks
Brendan Görreshttps://in.mathworks.com/matlabcentral/profile/authors/13254816-brendan-gorrestag:in.mathworks.com,2005:Question/4357362018-12-14T11:07:46Z2018-12-14T11:18:32ZSimulink Selector block with parameterised indices in masked Referenced ModelIn Simulink, I am trying to create a masked Referenced Model 'masked_X' whose input is one element of a single vector of several hundreds of signals. I would like to use a Selector block for this selection, with the Index specified as a field of a structured parameter (i.e. say p.p_index) of the mask. However, I get the following error when running the model:
For dimensions 1, parameter 'Indices' in 'masked_X/Y Selector': invalid setting in 'masked_X/Y Selector' for parameter 'Indices'.
It looks like I cannot pass the Selector Index as a mask parameter into the referenced model. I suspect this is because the Selector indices (and Input port size) are 'nontunable block parameters'; would that be correct?
I can provide the Index as a parameter in the MATLAB workspace, say p1, but that defeats my purpose of being able to use the single parameter p (with instances p(1), p(2), etc.) to distinguish between multiple instances of the Referenced Model.
Am I doing something obvious wrong or is there a way to select a signal from a vector in a parameterised way within a Referenced Model? Three alternatives I can see are:
Use a Demux signal outside of the Referenced Model and only take in the selected scalar signal. That means a lot of dead ends from the Demux block, because I may not need to use all individual signals.
Precede each Referenced Model with its own Selector block. This would double the number of blocks in the overall Simulink model.
Use a Library block for 'masked_X' instead of a Referenced Model. This works fine, but has some other disadvantages (not able to view scopes inside model without disabling the library link first, etc.).
Any suggestions are welcome, or pleae let me know if any clarification is required.
Best regards,
Gerrit.Gerrithttps://in.mathworks.com/matlabcentral/profile/authors/758195-gerrittag:in.mathworks.com,2005:Question/4357392018-12-14T11:18:16Z2018-12-14T11:18:16ZHow to use CROSS with 3x1 Matrix on a 3xN MatrixHello,
I have 3x1 Matrix and want to do the CROSS of each element of a 3xN Matrix, to receive a 3xN Matrix as results.
Can I do this without a loop?
Thanks in advance!Marcus Whttps://in.mathworks.com/matlabcentral/profile/authors/14357430-marcus-wtag:in.mathworks.com,2005:Question/4356942018-12-14T07:23:21Z2018-12-14T11:17:34ZQuestion on using addedge()How do we use addedge to assign weights to a graph containing edges with multiple edge weigths(Value1 and Value2, in my example)?
tail = [1 2 3 4];
head = [2 3 4 5];
G = graph(tail,head)
G.Edges.Value1 = ones(4,1)
G.Edges.Value2 = zeros(4,1)
G = addedge(G,5,6,1) %I want to assign 1 to Value1 to the new edge created between nodes 5,6
I obtain the following error.
Error using graph/addedge (line 139)
Unable to add weighted edges to an unweighted graph.
Any suggestions?
Deepa Maheshvarehttps://in.mathworks.com/matlabcentral/profile/authors/8575091-deepa-maheshvaretag:in.mathworks.com,2005:Question/4357382018-12-14T11:16:18Z2018-12-14T11:16:18ZHow to design 9-bit UP/DN counter in simulink ?Hi, I want to design 9-bit UP/DN counter in simulink by using DFF. so that when input signal is logic '1' the counter count UP(9-bit) otherwise count Down (9-bit).
Say the UP/DN counter is 9b, and its previous state is 110110101, then a UP count would result in 110110110 or the DN count would result in 110110100.
could anyone please help me how to design that kind of UP/DN counter in simulink by using DFF ?
ThanksSarfaraz Ahmedhttps://in.mathworks.com/matlabcentral/profile/authors/13589339-sarfaraz-ahmedtag:in.mathworks.com,2005:Question/4357232018-12-14T09:58:32Z2018-12-14T11:13:07Z'Array indices must be positive integers or logical values' error for directory useHello Everyone,
I am currently undergoing a project in which I must create a code that analyses a potentially infinite amount of data. As such, I am using a directory and the csvread function in conjunction. I have been told that the continued use of csvread in my code slows down the code and makes it less efficient so I am therefore trying to load the csvread function into the RAM and create a shortcut that links it to my directory. I, however, keep getting an error saying 'Array indices must be positive integers or logical values.' Please see the relevant section of my code attached. Thanks.
%% Import and Sort Data
files = dir('\\nask.man.ac.uk\home$\Vortex Shedding\Used Data\*csv');
[r inx] = sort({files.name});
files = files(inx);
(csvread(files(i).name)) == Signal
%% Calculate Turbulence Intensity
for i=1:length(files)
ave = mean(Signal);
Average(i,:) = ave(:,3);
RMS = sqrt((sum((((Signal)-mean(Signal)).^2)))/(length(Signal)));
RootMeanSquare(i,:) = RMS(:,3);
TurbulenceIntensity(i,:) = (RootMeanSquare(i)/Average(i))*100;
endJack Radcliffehttps://in.mathworks.com/matlabcentral/profile/authors/14366683-jack-radcliffetag:in.mathworks.com,2005:Question/4357372018-12-14T11:09:16Z2018-12-14T11:09:16ZSimulink Model for Linear Induction Motor (LIM)Can anyone help me to find the simulink model for Linear Induction Motor (LIM)?Sajjad Ahmedhttps://in.mathworks.com/matlabcentral/profile/authors/7974498-sajjad-ahmedtag:in.mathworks.com,2005:Question/4357262018-12-14T10:20:27Z2018-12-14T11:08:20Zcan any one give me mat lab code of E spline and Lanczo's interpolation without inbuilt codeI am working on Interpolation in Image processing. For this I need fundamental interpolation algorithm matlab code without 'inbuilt' function. My paper is 'Fast digital zooming system using directionally adaptive image interpolation and restoration'.hena sharmahttps://in.mathworks.com/matlabcentral/profile/authors/12227302-hena-sharmatag:in.mathworks.com,2005:Question/4357352018-12-14T10:59:49Z2018-12-14T11:08:09Zhow could i animate Fourier series in matlab?Fourier series is this:
for solving i assume :
f=sin(x);
g=cos(x);
N=10;
l=10;
n=1:N;
HADIMARGOhttps://in.mathworks.com/matlabcentral/profile/authors/14322242-hadimargotag:in.mathworks.com,2005:Question/4354012018-12-12T16:09:12Z2018-12-14T11:06:34ZHow can ı do spike sorting to filtered EEG signal.I have this sample and ı need to do spike sorting. I start with finding thresholding value and want to see changes with this code. I have trouble with plotting in for loop and also further steps. I am very new in Matlab. If you can help it would be great thank you.
for k = 1:0.1:5
omg = median(abs(samp)/0.6745);
Thr =k * omg;
[pks, locs] = findpeaks(samp);
findpeaks(samp, 'Threshold', Thr);
[r,NumPeaks] = size(pks);
NumPeaks
end
hold onkaan bakirciogluhttps://in.mathworks.com/matlabcentral/profile/authors/11825452-kaan-bakircioglutag:in.mathworks.com,2005:Question/4357112018-12-14T08:54:00Z2018-12-14T11:05:51ZHow to call vector in matrix with conditionLet's say:
A=[7 2 3 50;4 5 6 15;1 8 9 20;1 1 1 30]
A= 7 2 3 50
4 5 6 15
1 8 9 20
1 9 8 30
B=[1; 7]
B=[1
7]
Question: I wanna call only vector in column 4 of matrix A with the condition is: the value of matrix B have the same value of vector in 1st column of matrix A?
I hope the result like that:
result=[20; 30;50]
result=[20
30
50]
i try :
result=A(ismember(A(:,1),B,'rows'),4);
But, result=[50; 20;30]% it is not in order of vector in matrix B ????ha hahttps://in.mathworks.com/matlabcentral/profile/authors/10592576-ha-hatag:in.mathworks.com,2005:Question/4356852018-12-14T05:55:54Z2018-12-14T11:02:17ZHow to compare 5 cell arrays and get their contents in one cell array?Hello gyus,
I have a cell array of size 600*1 and 4 cell arrays: cell1(361*2), cell2(376*2), cell3(362*2) and cell4(351*2).
cell array(600*1) have overlap rows with all of these 4 cell arrays column1 rows;
I want to output a cell of size 600*4 with the 600 rows and 4 columns, which are the contents of the 4 cells column 2 contents if there is an overlap with their rows , if not put 0.
More specifically some rows in cell array( 600*1) may exist in all the rows of all the 4 cells or may exist in just some of them.
Inputs: cell array, cell1, cell2, cell3, and cell4
I appreciate any help plz!
Original cell:
'R-HSA-3000171:R-HSA-3000171'
'R-HSA-3000178:R-HSA-3000178'
'R-HSA-3000471:R-HSA-3000471'
'R-HSA-3000480:R-HSA-3000480'
'R-HSA-3000484:R-HSA-3000484'
cell1
'R-HSA-3000171:R-HSA-3000171' 'PDGFB'
'R-HSA-5419276:R-HSA-5419276' ' MRPS30,MRPL48,MRPS33'
cell2
'R-HSA-3000171:R-HSA-3000171' 'TTR'
'R-HSA-5655302:R-HSA-5655302' 'FGF1,FGF17'
cell3
'R-HSA-3000171:R-HSA-3000171' 'COL4A6,LAMA1,COL4A5'
'R-HSA-416482:R-HSA-416482' 'GNB2,ARHGEF16,ABR'
cell4
'R-HSA-3000171:R-HSA-3000171' 'COL11A2,COL3A1'
'R-HSA-5419276:R-HSA-5419276' 'MRPL1'
Iwanted_output:
chochohttps://in.mathworks.com/matlabcentral/profile/authors/9728424-chochotag:in.mathworks.com,2005:Question/4355622018-12-13T13:28:06Z2018-12-14T11:02:09ZAny GPU implementation of k-nearest neighbor search? Hi, I am developing a SPH (smoothed particle hydrodynamic) code of solving fluid equations in Matlab. I have successfully vectorized and implemented the code on GPU. The speed up is astonishing, ~10 times faster than CPU code. In the process I got invaluable suggestions from Matt J and Joss Knight for speeding up the code. Thank them for their wonderful suggestions.
In SPH, one has to find the neighbor particles in a given radius for every particles in the domain. I use the matlab function knnsearch for this purpose. Now the other part of the code (except neighbor search but solving the fluid equations) is so fast that the limiting part now is the knnsearch, which uses kdtree algorithm runing on CPU. It takes 85% percent of the runtime (see the following code profiler results)
The function 'knnCPU_kdtree_func' uses the matlab built-in function knnsearch with kdtree algorithm runing on CPU. The other functions are solving the real fluid equations runing on GPU only consumes 10% of the total time.
I wonder is there any GPU implementation of k-nearest neighbor search that I can free download and using as a function call in my matlab code? Many thanks.Hao Zhanghttps://in.mathworks.com/matlabcentral/profile/authors/2574469-hao-zhangtag:in.mathworks.com,2005:Question/1970462015-04-10T04:14:54Z2018-12-14T11:01:53ZInterpolating Frames of the VideoI have two videos of 1 min each. First video has 900 frames whereas second video has 600 frames. So I am trying to interpolate second video to have 900 frames. Can anyone tell me how this can be done?rohan guptahttps://in.mathworks.com/matlabcentral/profile/authors/5546954-rohan-guptatag:in.mathworks.com,2005:Question/4356992018-12-14T08:11:28Z2018-12-14T11:01:47Zproblems of this functioni want to draw this function
but my code has an error:
code:
x=linspace(-5,5,1000);
n=double(1:1:100);
l=2;
f=sin(x);
g=cos(x);
s=0;
landa=((n*pi)/l );
a=(2/l)*int(f.*sin((n*pi)/l)*x,0,l);
b=(2/(l*landa))*int(g*sin((n*pi*x)/l,0,l));
for n=1:1:1000
p=((a*cos(landa*t)+ b*sin(landa*t))*sin((n*pi*x)/l));
s=s + p;
end
plot (x,t,s)
error:
>> f1
Matrix dimensions must agree.
Error in f1 (line 8)
a=(2/l)*int(f.*sin((n*pi)/l)*x,0,l);
where is the problem of this error and code?HADIMARGOhttps://in.mathworks.com/matlabcentral/profile/authors/14322242-hadimargotag:in.mathworks.com,2005:Question/4357342018-12-14T10:57:47Z2018-12-14T11:00:11ZHow to change the value of a variable in a iterative loop?My code consists of 3 heat flow equations that needs to be solved. In one of those 3 equations, the mass of the fluid is variable, i.e. we dont know the mass and we have to iterate the code to get the exact mass.
Currently i am assuming the mass to be 1Kg/s in the beggining and starting the loop, but its consuming too much of time. Is there any other way by which i can speed up my calculation time by doing lesser number of iterations? Somya Ranjanhttps://in.mathworks.com/matlabcentral/profile/authors/11405173-somya-ranjantag:in.mathworks.com,2005:Question/2757102016-03-27T22:43:14Z2018-12-14T10:58:57ZWhat are the properties in a BoundingBox from regionprops?I'm trying to figure out what coordinates are being stored from a BoundingBox. For instance
L=logical(bw);
box=regionprops(L,'Area', 'BoundingBox');
box(2)
And the output
ans =
Area: 127
BoundingBox: [10.5000 11.5000 10 19]
What are those 4 values? Is it the [top left, top right, bottom left, bottom right] coordinates?Recaphttps://in.mathworks.com/matlabcentral/profile/authors/6988385-recaptag:in.mathworks.com,2005:Question/4342442018-12-06T14:58:03Z2018-12-14T10:55:10Zhow to color each individual bar a different color on my bar graph with my labels?There are five bars in my bar graph and each represent a color. I would like to code my graph to match the color the bar represents
%code
c=categorical({'red','green','blue','yellow','orange'}) %x-axis
occupied= [97 82 75 88 69]; %y-axis
figure
bar(c,occupied)
title('Parking Occupency')
xlabel('Decal')
ylabel('Closed Parking')
Shafali Bhardwajhttps://in.mathworks.com/matlabcentral/profile/authors/13387984-shafali-bhardwajtag:in.mathworks.com,2005:Question/4357332018-12-14T10:55:09Z2018-12-14T10:55:09ZBand-pass filter with designfiltI'm using designfilt to create a bandpass filter to meet a specification which has centre, 3dB and 20 dB frequency points.
The filter shape is narrower than a 2nd order butterworth filter, but wider than a 4th order filter.
The designfilt arguments include HalfPowerFrequency1 & 2, StopBandFrequency1 & 2, and StopBandAttenuation1 & 2.
My hope was that specifying these (with StopBandAttenuation... = 20) would work, but designfilt threw an error
d = designfilt('bandpassiir', 'StopbandFrequency1', 2400, 'HalfPowerFrequency1', 2424, 'HalfPowerFrequency2',
2436, 'StopbandFrequency2', 2460, 'StopbandAttenuation1', 20, 'StopbandAttenuation2', 20, 'SampleRate', 20000);
Error using designfilt (line 427)
(How) can I create such a filter?Mark Tyndallhttps://in.mathworks.com/matlabcentral/profile/authors/10682369-mark-tyndalltag:in.mathworks.com,2005:Question/4357292018-12-14T10:30:15Z2018-12-14T10:54:18ZNon-singleton dimensions of the two input arrays must match each other using bsxfun Hi,
I have 4D data with dimensions mydata=[ 156 , 192 , 26 , 41]. I have also an array of values which I import in Matlab as .txt file. The array has dimensions array=[1, 41].
I am using the bsxfun like this:
mydata = bsxfun(@rdivide, mydata, mydata(:,:,:,find(array==0)));
I am keep getting the error but I don't understand why. Can someone help with that?
Thanks a lot in advanced. Marhttps://in.mathworks.com/matlabcentral/profile/authors/4757715-martag:in.mathworks.com,2005:Question/4356752018-12-14T03:46:12Z2018-12-14T10:54:17ZImproved Euler's Method Code
Problem 1: Consider the following Initial Value Problem (IVP) where 𝑦 is the dependent variable and 𝑡 is the independent variable: 𝑦 ′ = sin(𝑡) ∗ (1 − 𝑦) with 𝑦(0) = 𝑦0 and 𝑡 ≥ 0 Note: the analytic solution for this IVP is: 𝑦(𝑡) = 1 + (𝑦0 − 1)𝑒^cos(𝑡)−1.
Part 1B: Approximate the solution to the IVP using the Improved Euler’s method with the following conditions: Initial condition 𝑦0 = − 1/2 ; time step ℎ = 1/16 ; and time interval 𝑡 ∈ [0,20]
+ Derive the recursive formula for the Improved Euler’s method applied to this IVP
+ Plot the Improved Euler’s method approximation
+ Plot the absolute error between the approximation and the exact solution using a semilog plotSarita Marchhttps://in.mathworks.com/matlabcentral/profile/authors/14147956-sarita-marchtag:in.mathworks.com,2005:Question/4341912018-12-06T10:13:29Z2018-12-14T10:50:52ZHow can I stich multiple images (about 45) in order to recreate a complete one?I have several images of a painting and i have to recreate the complete drawing by sticthing (or registrating?) the single images that have a partial overlap area.
I don't know which method is the best, imwarp, or stiching codes.
Thanks!! SofySofia Ceccarellihttps://in.mathworks.com/matlabcentral/profile/authors/14115450-sofia-ceccarellitag:in.mathworks.com,2005:Question/4357082018-12-14T08:49:18Z2018-12-14T10:50:23Zhow to decompose a polynomial Dear All,
For a given polynomial of the minimum order equals to 1, as follows:
F = mu1^2*x1^6 - 2*mu1*mu2*x1^6 + 6*mu1*mu2*x1^5*x2 - 6*mu1*mu2*x1^4*x2^2 + 2*mu1*mu2*x1^3*x2^3 + mu2^2*x1^6 - 6*mu2^2*x1^5*x2 + 15*mu2^2*x1^4*x2^2 - 20*mu2^2*x1^3*x2^3 + 15*mu2^2*x1^2*x2^4 - 6*mu2^2*x1*x2^5 + mu2^2*x2^6
where m1 and mu2 are coefficients.
Having defined X= [x1 ; x2];, how can I decompose 'F' to the following Structure:
F = X' * M * X; or F =X' * S' * S * X
Where M or S are matrices in terms of the powers of x1 and x2 or constant coefficients;
Is there any special matlac command for doing this?
many Thanks.Saeed rhmthttps://in.mathworks.com/matlabcentral/profile/authors/6205577-saeed-rhmttag:in.mathworks.com,2005:Question/4322252018-11-27T09:16:24Z2018-12-14T10:49:43Z Repeat operation and change values of an arrayHi everyone, i have an array P 1x3xn.
If the distance between point 1 and point 2 is different from t I have to change the Y of point 2 of Y(2) * f. If the distance between point 2 and point 3 is different from t I have to change the Y of point 3 of Y(3) * f. If the distance of point n-1 and point n is different from t I have to change Y of point n of Y(n) * f.
How can i do it? Thank you so much! Riccardo Rossihttps://in.mathworks.com/matlabcentral/profile/authors/14082681-riccardo-rossitag:in.mathworks.com,2005:Question/4357322018-12-14T10:47:58Z2018-12-14T10:47:58Zparfor with knnsearch, why is so slow?The matlab built-in function knnsearch uses kdtree method to find K nearest neighbors, it is very optimized. However I want to further improve its speed as this part now is the bottleneck of my whole code. So my idea is to send query points by chunks and call knnsearch in parallel using parfor, but I do not understand why the parfor version is even slower than sending the query points all in once, please help or giving some explanations. Thanks! The code I used for the testing are as follows:
clear;clc;close all
n_parallel=4;
if isempty(gcp('nocreate'))==1
pooljob=parpool('local',n_parallel);
end
N=1e5;
knn_K=50;
rx=rand(N,1);
ry=rand(N,1);
BucketSize_kdtree=50;
Mdl=KDTreeSearcher([rx ry],'Distance','euclidean','BucketSize',BucketSize_kdtree); %%% kdtree search object
idx_Neighbor_cell=cell(n_parallel,1);
d_Neighbor_cell=cell(n_parallel,1);
logic_beyor_cell=cell(n_parallel,1);
N_par=round(N/n_parallel);
tic;
[idx_Neighbor1,d_Neighbor1]=knnsearch(Mdl,[rx ry],'K',knn_K);
toc
tic;
rx_query_cell=mat2cell(rx,[N_par*ones(1,n_parallel-1) N-N_par*(n_parallel-1)],1);
ry_query_cell=mat2cell(ry,[N_par*ones(1,n_parallel-1) N-N_par*(n_parallel-1)],1);
parfor i=1:n_parallel
[idx_Neighbor_cell{i},d_Neighbor_cell{i}]=knnsearch(Mdl,[rx_query_cell{i} ry_query_cell{i}],'K',knn_K);
end
idx_Neighbor=cell2mat(idx_Neighbor_cell);
d_Neighbor=cell2mat(d_Neighbor_cell);
toc;
The results:
Elapsed time is 0.389322 seconds.
Elapsed time is 1.199775 seconds.
Could anyone test this code in you machine see if you get similar speed difference? Any suggestion on improve the code further is appreciated. Thanks!Hao Zhanghttps://in.mathworks.com/matlabcentral/profile/authors/2574469-hao-zhangtag:in.mathworks.com,2005:Question/4357252018-12-14T10:07:14Z2018-12-14T10:47:51ZHow to find a smooth normal distribution curveI have a 150 data points for which I would like to plot a normal distribution curve. My output curve is not a single smooth curve but a mesh of traingles and lines approaching a curve.
My input and code is as follows:
ERh1 = [2.4100 0.7501 0.1456 0.5070 0.7409 0.7514 0.3036 0.3070 0.2066 0.0898 0.7575 0.5958 0.2354 0.3612 0.0990 0.0111 0.2014 0.0637 0.1760 1.5535 0.5505 0.2500 2.3364 0.0583 0.2947 0.1027 0.2261 0.2467 0 0.4858 0.7506 0.1006 0.0222 0.5000 0.8995 1.0034 0.3442 1.5742 0.2582 0.5012 0.8043 0.5822 0.4564 0.3887 0.1867 0.0532 0.7507 0.2714 0.5439 0.6896 0.2500 0.2423 0.5000 0.0108 0.1105 0.0744 0.1585 1.4920 0.2425 0.4498 1.2819 0.2325 0.5324 2.5002 0.0892 0.5095 2.7947 0.2572 0.0771 0.2486 0.5351 0.4665 0.2583 0 0.5307 0.0002 0.7683 2.3533 0.2710 0.2686 0.2387 0.3315 0.1607 1.4644 0.0261 0.4436 4.5000 1.1756 0.4178 0.2143 0.1008 0.5376 0.7417 0.6419 0.5216 0.4925 0.2726 3.2165 0.2500 0.4827 0 0.2354 0.5019 0.6584 0.5923 0.1998 0.2475 0.0946 1.0335 0.2555 0.0382 0.4665 0.5382 0.2500 0.4488 0.1915 0.2485 0.7947 0.2792 0.2916 0.4648 0.7443 0.2409 0.8798 0.3597 0.6237 0.0095 0.2500 0.2630 0.2500 0 0.2406 0.4855 1.4943 1.3022 0.4345 0.4298 0.0041 0 1.5033 0.1896 0.7646 0.5831 0.0335 0.7673 0.2467 0.3377 1.1875 0.4497 0.1678]
norm = normpdf(ERh1,mean(ERh1),std(ERh1));
plot(ERh1,norm)
I have been trying for a few days now to figure this out and not getting good results. Anyone with help, it will be greatly appreciated!
Wimpie Heathhttps://in.mathworks.com/matlabcentral/profile/authors/14366608-wimpie-heathtag:in.mathworks.com,2005:Question/4357312018-12-14T10:43:52Z2018-12-14T10:44:53ZCreate a new matrix from twoHello everyone, i have 2 matrix A and B:
A (4X8):
0 0 0 0 0 1 0 0
0 0 1 1 1 0 1 0
0 1 0 0 0 1 0 1
1 0 1 0 0 1 0 0
B (4X3):
X1 Y1 Z1
X2 Y2 Z2
X3 Y3 Z3
X4 Y4 Z4
is it possible to create a new matrix like this?
C:
X4 Y4 Z4 X3 Y3 Z3 X2 Y2 Z2 X2 Y2 Z2 X1 Y1 Z1 X2 Y2 Z2 X3 Y3 Z3
X4 Y4 Z4 X3 Y3 Z3
X4 Y4 Z4
i tried with this code but it creates a matrix with only 3 columns:
for k=1:8
C=B(A(:,k),:);
if ~(mod(k,1))
dlmwrite('file.txt',C,'-append');
READC=dlmread('file.txt') ;
end
end
Thanks!Riccardo Rossihttps://in.mathworks.com/matlabcentral/profile/authors/14082681-riccardo-rossitag:in.mathworks.com,2005:Question/4355592018-12-13T13:03:21Z2018-12-14T10:44:27ZAdd row and column headers to 3d matrixHi
I'm trying to find a simple solution to add column and row headers. So I have:
Z(164,167,59) -3d matrix
names(1x164)-row header
names2(1x167) -column header
I tested to see if i can do a 2d and was going to add a loop using a horzcat as such:
ZHDR=[names2;Z(:,:,1)]
Thank you for any help.
Kevin
Kevin Tehhttps://in.mathworks.com/matlabcentral/profile/authors/856093-kevin-tehtag:in.mathworks.com,2005:Question/4357062018-12-14T08:30:20Z2018-12-14T10:41:47ZHow to execute multiple m files in current project from command line in windows?Hi,
I have got a large simulink project an multiple m files to run in this project. At the moment I start to first m file which loads the project with the following command
matlab -r "run('matlabfile1.m')"
in windows command line. When matlabfile1 finished the file closes itself but not the simulink project. In the next step I would like to run the second (and more) m file in the same project.
I have the following ideas:
run a matlab file automatically when opening it. Perhaps there exists a way to implement that
run the next matlab file in a new matlab instance and communicate between the matlab instances so that the file is able to run the simulink project
run a seperate matlab file in the background which is watching for commands (perhaps from windows command line) to run different matlab files
Are there some ideas to solve my problem?Sandrahttps://in.mathworks.com/matlabcentral/profile/authors/7034415-sandratag:in.mathworks.com,2005:Question/4357302018-12-14T10:33:48Z2018-12-14T10:33:48ZI want to turn the following text into a MATLAB code ; can you help me ?Compute Cmax. To do this, C is computed from Eq. 3-60 for different
values of yr, starting with an initial yr value of approximately 1.0 and
continuing until a maximum value of C is reached
C = [ 1 /( Yr + 2 Br ( Yr 1 )) ] [ ( m / Yr )^2 + ( ( 1-m ) / ( Yr 1) )^2 ( 1 / 2 Br ) ] +
[ 2 m ( 1 m ) / ( 3 ( Yr + 2 Br ( Yr -1 ) ] [ 5 / Yr ( Yr -1 ) 2 / ( Bf + Yr -1 ) ]
[ ( m / Yr ) ( ( 1 m ) / ( Yr 1) ) 1 / 2 Br ]nilou mhyhttps://in.mathworks.com/matlabcentral/profile/authors/14366548-nilou-mhytag:in.mathworks.com,2005:Question/4347202018-12-09T12:07:28Z2018-12-14T10:28:01ZEEGLAB Biosig toolbox: problem importing markers correclty from .vmrk fileHello fellows!
I am doing EEG analysis using the EEGLAB toolbox for treating data from BRAINVISION acquisition software. The BRAINVISION software outputs 3 files: '.eeg' - the continuous data - '.vhdr' a header file annd finally a '.vmrk' triggers markers file.
In order to import everything in EEGLAB, I use the BIOSIG importing tool in EEGLAB as such (eeglab1.png):
An then I save my dataset using file > save current dataset as.
Now we get to the issue. My .vmrk file looks like this (vmrk.png):
with the first relevant marker being the 4th one, 'S 11'
The problem is that this vmrk file is not imported correctly in eeglab via BIOSIG, and looks like this (eeglab2.png and eeglab3.png):
As you can observe the latency and duration are imported correclty, but the triggers denomination (1, 1, 11, 12, 13, etc...) are not, and particularly my trigger '11', which should be at position 4, is shifted to position 140, and hence all my triggers are shifted. Of course, since there are only 410 triggers - because my original .vmrk file contains only 410 triggers positions - , this shifting causes any additionnal triggers beyond position 410 to be ignored, meaning that additionnally to the shifting, mroe than half my triggers are missing.
I tried to use either older or most recent versions of biosig in order to see if that changed, but it doesn't help, the problem remains.
Any idea on how to fix this issue?
Thank you and cheers,
Fred
Frederic Freundlerhttps://in.mathworks.com/matlabcentral/profile/authors/5798678-frederic-freundlertag:in.mathworks.com,2005:Question/4352802018-12-12T05:23:01Z2018-12-14T10:27:58ZI want to increase/decrease volume of audio file using the slider,is it necessary to run audio data from slider?global filename;
[y,Fs] = audioread(filename);
slider_val = get(hObject, 'Value');
if slider_val == 0
x = y/5; % Decrease volume
player = audioplayer(x, Fs);
play(player,[1 (get(player, 'SampleRate')*3)]);
elseif slider_val == 1
x = y*5; % Increase volume
player = audioplayer(x, Fs);
play(player,[1 (get(player, 'SampleRate')*3)]);Madhav Sapkotahttps://in.mathworks.com/matlabcentral/profile/authors/14293807-madhav-sapkotatag:in.mathworks.com,2005:Question/4355112018-12-13T07:45:23Z2018-12-14T10:25:41ZHow to remove center traingle in a circle image?How do I remove a center triangle of a circle image? Can any one share ideas?Selva Karnahttps://in.mathworks.com/matlabcentral/profile/authors/4733665-selva-karnatag:in.mathworks.com,2005:Question/4357002018-12-14T08:16:10Z2018-12-14T10:21:10ZBirkhoff-von Neumann decomposition of nonnegative doubly stochastic matricesHi, I have the following problem: Given a (n \times n) doubly stochastic matrix Q with strictly positive elements, which is a convex combination of \factorial{n} permutation matrices, I want to find out the strictly positive vector of mixing weights. Thanks!valentino dardanonihttps://in.mathworks.com/matlabcentral/profile/authors/11241617-valentino-dardanonitag:in.mathworks.com,2005:Question/2493042015-10-18T15:27:02Z2018-12-14T10:20:25ZSet y-axis as vertical when plotting in 3dBy default, the 'plot3' function assumes that Z-data denotes vertical data. For example, if I were plotting motion capture data of, say, a subject's right foot - plot3(rFootX, rFootY, rFootZ) - it would plot the 'rFootZ' data as the vertical position of the foot.
Is there a way to set 'plot3' so that it uses Y as the default vertical axis? That is, if my motion capture system outputs the vertical position of a subjects' right foot as 'rFootY,' can I set things up so that plotting 'plot3(rFootX, rFootY, rFootZ)' displays the data correctly (with Y pointing up)?
Up until now, I have been using a little helper function to get things to plot with Y pointing up:
function plot3y(x,y, z,varargin)
plot3(x,-z,y,varargin{:})
But this gets VERY confusing when trying to manipulate the resulting plots using the default Matlab functions like "axis" and "surface," as these assume that the Z-axis is the vertical.
I have also played around with using camroll (specifically, 'camroll(-90)'), but this tends to get screwy when I start adjusting the the viewpoint (the camera still tries to rotate around a Z-vertical axis).
tl;dr - I'm looking for some setting that I adjust that will set the Y-axis as the default vertical when using 'plot3'Jon Matthishttps://in.mathworks.com/matlabcentral/profile/authors/7135648-jon-matthistag:in.mathworks.com,2005:Question/4356422018-12-13T21:12:14Z2018-12-14T10:20:11ZMutli Degree of freedom questionHi there!
Im trying to use the following code to solve a problem. Yet I keep getting an error code on Line 9: ydot(1) = y(2); stating not enough input arguments.
This is for a multi degree of freedom analysis. What am i doing wrong?
Thanks in advance!
function ydot = Assignment_Q2_D(t,y,m,c,k)
m = 0.5;
c = 0.5;
k = 200;
omega = 33;
ydot = zeros(6,1);
ydot(1) = y(2);
ydot(2) = (1/2*m) *( -c*y(2) - 4*k*y(1) + k*y(3) + 2*k*y(5) );
ydot(3) = y(4);
ydot(4) = (1/2*m) * (-3*c*y(4) + c*y(6) + 2*k*y(1) +k*y(3) - 4*k*y(5) +( 50*sin(omega*t)));
ydot(5) = y(6);
ydot(6) = (1/m) * ( c*y(4) - 2*c*y(6) + 2*k*y(1) + k*y(3) - 4*k*y(5) );
endKyle Spencerhttps://in.mathworks.com/matlabcentral/profile/authors/14146242-kyle-spencertag:in.mathworks.com,2005:Question/4356332018-12-13T20:08:01Z2018-12-14T10:20:11ZPlz help number 2,3 coding
write number3 coding. plz check my coding
x=linspace(0,8,50);
y=x.*exp(-x.^0.8)*0.2;
trapz(x,y,z)
plot(x,y)
Krisendhttps://in.mathworks.com/matlabcentral/profile/authors/13531736-krisendtag:in.mathworks.com,2005:Question/4355882018-12-13T16:40:53Z2018-12-14T10:17:27ZModified method from superclass isn't calledHi,
I'm trying to extend the inputParser class, and hereby also extend the method parse() from that class.
Is it at all possible? The documentation seems to explain how to do this:
https://se.mathworks.com/help/matlab/matlab_oop/modifying-superclass-methods-and-properties.html
Here is my subclass with extended method:
classdef myInputParser < inputParser
properties
Extra
end
methods
% Class constructor
function obj = myInputParser()
obj.Extra.test = 0;
end
% Extend inputParser superclass method parse()
function parse(obj, varargin)
% preprocessing steps
parse@inputParser(obj, varargin{:});
% postprocessing steps
obj.Extra = obj.Results;
end
end
end
I create the object, and parse the input like so:
p = myInputParser;
p.parse(varargin{:})
When calling the parse() method on the object, it doesn't seem to call the extended parse() in the subclass.
What am I missing? Any help would be greatly appreciated.No Namehttps://in.mathworks.com/matlabcentral/profile/authors/11432202-no-nametag:in.mathworks.com,2005:Question/4356322018-12-13T19:44:48Z2018-12-14T10:17:21ZProblems with plotting multiple objects
Hi! I have a question. There are the label of contour on the rectangle. Can I delete this for a better reading? Thank you
This is the code for the graphic part.
[c,cc]=contour(x,y,u',[13.1,13.5,14,14.5,15,15.5,15.9]);
clabel(c,cc,'LabelSpacing',72,'Color','r','FontWeight','normal');
rectangle('position',[0,0,Lx,Ly])
rectangle('position',[x(6),y(4),d,3*d],'FaceColor',[0.5 0.5 0.5],'LineWidth',1)
axis('equal');
set(cc,'ShowText','on');
colormap cool;
axis([0 Lx 0 Ly]);
hold on;
streamline(x,y,qx,qy,[x(2:4),x(9:nx)], y(ny+1)*ones(1,6));
quiver(x(2:5),y(2:ny),qx(2:ny,2:5),qy(2:ny,2:5),'b');
quiver(x(8:nx),y(2:ny),qx(2:ny,8:nx),qy(2:ny,8:nx),'b');
quiver(x(6:7),y(2:3),qx(2:3,6:7),qy(2:3,6:7),'b');
trimesh(T,P(:,1),P(:,2),hh);
title('Sheet pile wall');
xlabel('x');ylabel('y');zlabel('z');MICHELA VOLGAREhttps://in.mathworks.com/matlabcentral/profile/authors/13760896-michela-volgaretag:in.mathworks.com,2005:Question/4356642018-12-14T01:49:46Z2018-12-14T10:16:58ZMATLAB saves file with ^M end of lineI worked with MATLAB in Linux. Recently, I edited a text file in MATLAB, and found the file was saved by MATLAB with the dos/Windows end of line ^M. Anyone can help with the MATLAB setting to enable again the UNIX newline? Qi Daihttps://in.mathworks.com/matlabcentral/profile/authors/14364615-qi-daitag:in.mathworks.com,2005:Question/4353572018-12-12T13:07:02Z2018-12-14T10:16:45ZHow can matlab understand that the external button is pressed or not?Hi,
I want to rotate the servo motor using Arduino Uno, Matlab 2017b and with an external push button. So it's a matter of servo control with Arduino through Matlab. What I have written is attached. It doesn't have any bugs but I cannot reach the desired result, it prints just "The button is not pressed".
The logic of my code is that at first the button is not pressed and then when it is pressed some counter is responsible for next ifs. When the button is pressed for the first time, the counter will be equal to 1 and shaft must rotate according to the code. After that, we have unpressed button and again Button ==0. After pressing again, the value of counter will be 2 and the next tasks. But it doesn't work.
What is wrong with this code?
Thank you in advance.Nazanin Fazelniahttps://in.mathworks.com/matlabcentral/profile/authors/13893746-nazanin-fazelniatag:in.mathworks.com,2005:Question/3890692018-03-18T17:02:28Z2018-12-14T10:11:47ZHow do you export a plot into a stl file?t = [0:0.1:2*pi]
a = sin(t);
plot(t,a)
Trying to export this sin plot as an stl. file. I may need to make it 3d first.
Any ideas?
Sophia Smithhttps://in.mathworks.com/matlabcentral/profile/authors/12423369-sophia-smithtag:in.mathworks.com,2005:Question/3937482018-04-09T18:10:45Z2018-12-14T10:10:31ZAugmented image data store wont work unless i write Augmented image Source, is this stopping me from making a data store?Compile errors says:
Error using trainingOptions (line 187)
The value of 'ValidationData' is invalid. Validation data must be a table, an ImageDatastore, or a cell
array with input data and responses.Nathan Reillyhttps://in.mathworks.com/matlabcentral/profile/authors/12169608-nathan-reillytag:in.mathworks.com,2005:Question/4356502018-12-13T22:53:18Z2018-12-14T10:06:54ZComparing two input voltages?I'm attempting to build a ride by wire system, I've built a proof of concept in Simulink utilising an Arduino as the controller.
Now I've moved on to real hardware with a RBW throttle, it uses two hall effect sensors to give two voltages. On one it gives a range of 0.9 - 4.4v and the other is 0.4 - 2.2v. I've looked through various blocks but I'm struggling to see a way forward and would appreciate any guidance on this problem.Philip Harrishttps://in.mathworks.com/matlabcentral/profile/authors/14178138-philip-harristag:in.mathworks.com,2005:Question/4357242018-12-14T10:02:58Z2018-12-14T10:02:58ZSplitting column to different length columns in matrixHello, for example with the function below, I know that it is possbile split a column to same length columns. If we have 70 values, after this process we get 7x10 matrices.
a=reshape(a,10,[])
But my aim is for example, we have 30 values and I want to split them like;
column 2.column 3.column
5 values 12 values 13 values
Is it possible?
Thanks in advance.Hüsamettin Taysihttps://in.mathworks.com/matlabcentral/profile/authors/12160415-husamettin-taysi