I've created MATLAB code here that produces a graph and depending on parameters (some random) converges to a different a

I want to be able to run this 100 times for a certain set of parameter values and take an average of the amount of groups that it produces.

Here is the code I have at the minute. Do I need to create another m file for this or can i do it within the function I've already created? [code]function [t seqBeliefs] = extendedHK(n, tol, adj) %extendedHK Summary of function goes here %Detailed explanation goes here beliefs = rand(n,1); seqBeliefs = beliefs; %NxT matrix converge = 0; step = 0 t = step while converge ~= 1 step = step+1; t = [t step]; A = zeros (n,n); for i=1:1:n for j=i:1:n if abs(beliefs(i) - beliefs(j)) < tol && adj(i,j)==1 A(j,i)=1; A(i,j)=1; end end end beliefs = A*beliefs./ sum(A,2); seqBeliefs = [seqBeliefs beliefs]; if sum(abs(beliefs - seqBeliefs(:,step)))<1e-12 converge = 1; end end plot(t,seqBeliefs) end %%in command window type adj=random_graph(n) then call extendedHK function %%with same n then tol value and 'adj'[/code]

How can I vectorize these loops in Matlab?

[code]function[Y] = busadmittance(z) ne = z(:,1); nt = z(:,2); r = z(:,3); x = z(:,4); lines = length(ne); buses = max(max(ne), max(nt)); Z = r + 1j*x; y = ones(length(Z),1)./Z; Y = zeros(buses,buses); [/code] How can I vectorize below 'for' loops, that follows the above code?
[code]for k = 1:buses for l = 1:lines if ne(l) == k || nt(l) == k Y(k,k) = Y(k,k)+ y(l); end end end for k = 1:lines if ne(k)>0 && nt(k) > 0 Y(ne(k),nt(k)) = -y(k); Y(nt(k),ne(k)) = -y(k); end end[/code] are there any additional code enhancements apart from preallocation and vectorization, that can greatly affect the performance? (forget gpu, cluster)

Add new column with header to csv Matlab

I have an csv file with column headers (strings) and data. I then want to add a new column with a specific header 'My new header' and data is stored in a 100x1 double variable called mynewdata. How I can add a new column with header 'My new header' and values of the column be mynewdata?

MATLAB nested loop

[code]function[output]=tridiag(d,l,r) A=zeros(3); for i=1:5 A(i,i)=d(i); for j=2:5 A(j,(i-1))=l(j); for k=1:4 A(k,(i+5))=r(k); end end end A end[/code] on this part where it says,
[code]for j=2:5 A(j,(i-1))=l(j);[/code] I want the variable "i" to execute from 1 ~ 4. and not 5. That's why I put i-1 there, but obviously it won't work.

How can I make the nested j-loop to repeat i=1:4, instead of i=1:5? Same question goes for k-loop too.

+update: So I want the outside i-loop to perform from 1:5, but for the inner j-loop, i want i=1:4.

Get the value of an array from another array in Matlab

I have an array A 20x1 double and another array B which is 1000x1 double. I want to set array C where array C will be 1000x1 double where the first value in it is equal to the C[1] = A[B[1]] and so on like C[i] = A[B[i]] how this can be done?

How to integrate an element in an image according to the intensity?

I have a squared image img that I cut in two parts vertically, I calculate the mean of intensity in each part and then I compare these means with minimal function. I create a square of one with a size 200 by 200.

My question is : How can I say that I want place the square in the part of the image where there is the minimal mean of intensity (so once it can be in the right part and an other once it can be in the left part and the place in the part is not significant at first)? I wold like to do this automatically because I have several images.
[code]img=imread('image.tif') dim=size(img,1); meam1=mean(mean(:,1:dim/2)); mean2=mean(mean(:,dim+1/2:end)); Min=min(mean1,mean2); Square= ones(200,200);[/code]

Sending specific time values from matlab to arduino

I am reading a audio signal from a file in matlab. I used a stft on it, squared the magnitudes and summed all of them togther. Bascially, I have one power value per frequency spectrum. And I am plottign all of these values. So, what i have now, is that I have the power spectrum from my signal.

Now I was trying to find the x-y coordinates from the peaks in my plot. So that I know at which time, which power value is there. And then I was also trying to find the time distance between those peaks. How much time is gone, until the next peaks come

There is another problem: I have very high power values. The range differs from 50000 to 120000. So what I did, is converting all of those values to percent. Btw, what i am doing here is, using the formel from the linear interpolation. So I have a range from 0 to 100 percent now. I wrote program, which is sending signals from matlab to arduino uno, which controls a vibration motor.

But how can I say him, when and how strong my motor should vibrate and how long he should wait until the next peaks comes ?? Where do I have to define that and why? I have very low experience with arduino, so I had some help.

EDIT:

The converted power values to percent, are saying how strong it should vibrate and at which time.

100 --> strong vibration 10 --> weak vibration

I thought about using PWM for Arduino, since it is converting signals to percent. But I want to send my percent values to arduino.

Plot the cylinder in Matlab

can anyone help me to solve the task: Plot the cylinder ((x-1)^2)/4 + y^2=4 for -1<=z<=1 by using the cylinder function. The cylinder shall begin in z=-1 and end in z=1

I have read through the help cylinder function in Matlab but cant seem to figure out how to get it right.

I tried to put figure [X,Y,Z]= cylinder((x-1)^2/4+y^2) surf [X,Y,Z] axis square

An array of variable-length arrays of integer numbers in matlab

I want to create an array of variable-length arrays of integer numbers in matlab. In fact i want to create a set of paths and I want to have the index of min path in the end of it. I tried to solve it with cell in matlab but i can't solve it in a good way. Can someone propose a better solution?

My solution:[code]total=numel(Graph(:,1)); path=zeros(1,total); paths=cell(total); for i=1:total for j=1:total if(path(index)==Graph(j,2)) if(ismember(Graph(j,1),S)==true) index=index+1; path(index)=Graph(j,1); count=count+1; end end lengths(jj)=count; end for kk=1:numel(path) paths(i,kk)={path(kk)}; end end[/code]

Export R list into Matlab cell-array

I have searched everywhere and I'm currently unable to find a solution to my issue.

I have a list created under Rstudio which is made of matrices list(mat1,mat2,...). What i would like to do is export that list to matlab to get a cell-array with each cell containing 1 matrix. Does anyone has any idea how to proceed ? I have used the R.matlab package already but when I import the resulting file in matlab I obtain a "structure" file which seems to be empty. following is my code to export.

W1 is my list.
[code]library('R.matlab') writeMat("W1.mat",W1export=W1)[/code] Thanks
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir