https://www.mathworks.com/matlabcentral/answers/questions.atomMATLAB Answers — New Questions2016-02-07T15:23:45Ztag:www.mathworks.com,2005:Question/2669932016-02-07T14:37:56Z2016-02-07T15:23:45ZHow to use a struct array field as input in function<p>Hi!
I'm sorry if this question have already been asked, but I can't figure how to do this.</p><p>So here's what I'm trying to do :</p><p>I have a struct array with 4 fields inside it</p><p>LittlePoney.Blue;</p><p>LittlePoney.Purple;</p><p>LittlePoney.Green;</p><p>LittlePoney.Red;</p><p>I would like to create a function which use the field as an input. Like This :</p><p>function [A] = BigBrother(Field)</p><p>LittlePoney.Field;</p><p>end</p><p>If someone can help me with this I'd be really happy.</p><p>Waiting for your answers
Thank you</p>Jerome Lafrancehttps://www.mathworks.com/matlabcentral/profile/authors/7511628-jerome-lafrancetag:www.mathworks.com,2005:Question/2669582016-02-07T06:11:24Z2016-02-07T15:22:35ZHow to use fminbnd correct Re: steepest descent method<p>I am trying to code a steepest descent problem. I have the main file:</p><p>I have edited my file and combined the two functions into one .</p><p>The objective function is "c". The variables in this problem are q, t and alpha. q and t appear in "c", while alpha appears in the re-expressed anonymous function based off "c" in the later part of the code.</p><p>I'm trying to minimize c using the steepest descent problem. First I find the gradient (partial differential equations of c with respect to q and t), then i use subs and double to generate new values of q and t with each iteration in the while loop. I also use fminbnd as a line search to find the value of the step size, "alpha" that minimizes the objective function during each iteration.</p><pre class="language-matlab">syms q t alpha
</pre><pre class="language-matlab">a = 0.20; %annual fixed charges, fraction
c_c = 12.50; %crude oil price, $/kL
c_i = 0.50; %insurance cost, $/kL
c_x = 0.90; %customs cost, $/kL
i = 0.10; %interest rate
n = 2; %number of ports
p = 7000; %land price, $/m2
</pre><pre class="language-matlab">c = c_c + c_i + c_x + ...
+ (2.09e4*t.^-0.3017) / 360 ...
+ (1.064e6*a*t.^0.4925) / (52.47*q*360) ...
+ (4.242e4*a*t^0.7952) / (52.47*q.*360) ...
+ (1.813*i*p*(n*t+1.2*q).^0.861) / (52.47*q*360) ...
+ 4.25e3*a*(n*t+1.2*q) / (52.47*q*360) ...
+ (5.024e3*q.^-0.1899)/360 + (0.1049*q.^0.671)/360;
</pre><pre class="language-matlab">k=0; % iteration counter start for outer loop
eps=1; %tolerance for outer loop
</pre><pre class="language-matlab">x=[150000;450000]; %initial starting point
</pre><pre class="language-matlab">dq=diff(c,q); %equation of partial differentiation of c wrt q
dt=diff(c,t); %equation of partial differentiation of c wrt t
</pre><pre class="language-matlab">alpha = 100000; %initial step size for outer loop
</pre><pre class="language-matlab">while eps>1e-10 & k<10000 %tolerance and number of iterations
</pre><pre class="language-matlab">eps=double(abs(subs(dq, [q,t], [x(1),x(2)]))+abs(subs(dt, [q,t], [x(1),x(2)])));
</pre><pre class="language-matlab">% I used subs & double to make the partial differentiation terms dq and dt
% scalar to calculate eps.
</pre><pre class="language-matlab">a1 = x(1) - alpha*double(subs(dq,[q,t],[x(1),x(2)]));
a2 = x(2) - alpha*double(subs(dt,[q,t],[x(1),x(2)]));
</pre><pre class="language-matlab">[alpha1,fval] = fminbnd(@(alpha) (c_c + c_i + c_x + ...
+ (2.09e4*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)]).^-0.3017) / 360 ...
+ (1.064e6*a*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)]).^0.4925) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])*360) ...
+ (4.242e4*a*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)])^0.7952) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)]).*360) ...
+ (1.813*i*p*(n*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)])+1.2*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])).^0.861) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])*360) ...
+ 4.25e3*a*(n*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)])+1.2*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])*360) ...
+ (5.024e3*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)]).^-0.1899)/360 + (0.1049*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)]).^0.671)/360))))))))))))))))))))))))))),10,120000)
</pre><pre class="language-matlab">alpha = alpha1;
</pre><pre class="language-matlab">x(1)=a1;
x(2)=a2;
</pre><pre class="language-matlab">x
</pre><pre class="language-matlab">k=k+1;
</pre><pre class="language-matlab">end
</pre><p>The problem is that I can get the code working, but the values of q, t and alpha are changing extremely slowly in each iteration.</p><p>I'm also having problems with "alpha" - in theory, "alpha" is supposed to change with each iteration, however my alpha value always takes on the maximum value (upper bound) in fminbnd and remains stationary.</p><p>If I remove this portion of the code:</p><pre class="language-matlab">[alpha1,fval] = fminbnd(@(alpha) (c_c + c_i + c_x + ...
+ (2.09e4*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)]).^-0.3017) / 360 ...
+ (1.064e6*a*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)]).^0.4925) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])*360) ...
+ (4.242e4*a*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)])^0.7952) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)]).*360) ...
+ (1.813*i*p*(n*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)])+1.2*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])).^0.861) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])*360) ...
+ 4.25e3*a*(n*(x(2)-alpha*double(subs(dt,[q,t],[x(1),x(2)])+1.2*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])) / (52.47*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)])*360) ...
+ (5.024e3*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)]).^-0.1899)/360 + (0.1049*(x(1)-alpha*double(subs(dq,[q,t],[x(1),x(2)]).^0.671)/360))))))))))))))))))))))))))),10,120000)
alpha = alpha1;
</pre><p>The convergence rate increases greatly. Any idea as to what may be causing this issue?</p>Jimhttps://www.mathworks.com/matlabcentral/profile/authors/7631110-jimtag:www.mathworks.com,2005:Question/2669822016-02-07T11:23:02Z2016-02-07T15:21:43ZCount the 'duration' of integer values appearing sequentially in a list (viterbi path)<p>I have a long (~250 000) sequence of integers between 1 and 10 which represent a sequence of states (viterbi path) which is the output of a hidden markov model.</p><p>I would like to get the durations of each individual state and put these into a matrix (or perhaps several vectors as they may have different lengths, I'm not sure how to deal with this).</p><p>an example vpath might be [1 1 1 2 2 3 3 2 2 1 1 1 1 1 4 4 4] and I would like to return something like so</p><pre class="language-matlab">3 5 ##first and second durations of state 1
2 2 ## first and second durations of state 2 etc.
2
3
</pre><p>Here is the code I have so far (the appraoch is really ugly and gives me weird numbers at the beggining and end, sorry that you have to look at it):</p><pre class="language-matlab">%should return array of length K with ave. number of samples per state visit
function visits = state_duration(vpath, K)
visits = [];
</pre><pre class="language-matlab">a = zeros(length(vpath), K);
b = ones(K,1);
visits.avgs = zeros(K,1);
</pre><pre class="language-matlab">for x = 1:K
for i = 1:length(vpath)
if vpath(i)==x
a(b,x)=a(b,x)+1;
else
b(x)=(b(x)+1);
end
end
rws = a(2:end,x);
visits.avgs(x)=mean(rws(rws~=0));
end
visits.full = a(2:end,:);
end
</pre><p>**if anyone can think of a better way to phrase this question let me know</p>Alexander Morleyhttps://www.mathworks.com/matlabcentral/profile/authors/7631669-alexander-morleytag:www.mathworks.com,2005:Question/2669982016-02-07T15:20:16Z2016-02-07T15:20:16ZHow do I fix a block mask error?<p>In the data acquisition toolbox of simulink, when I place an "Analog Input" block into my model, and then double click on it, I get an "Error opening the Mask dialog". A sampling of other blocks in the DAQ toolbox and Instrument Toolbox does not reveal the same issue.</p><p>Recently had to do a CHKDSK /R (repair) on my computer, due to four bad disk sectors that was prohibiting correct boot up. So it is possible that some of the Matlab files where corrupted in process.</p><p>Do I need to do a complete reinstall of the MATLAB/Simulink software or is there another less intrusive means to correct the problem?</p>Weshttps://www.mathworks.com/matlabcentral/profile/authors/5019786-westag:www.mathworks.com,2005:Question/2669972016-02-07T15:08:45Z2016-02-07T15:08:45ZTrying to use MatLab to read in an AVI frame by frame. The files were created by sony 'Media File Player v1.6.5.56'. I am able to get the first frame, but the second frame read generates an error.<p>The error it returns is;</p><p>Error using VideoReader/readFrame (line 111) Unable to read the file. The file appears to be corrupt.
The AVI file plays using other readers. I do see that 'Media File Player' has a history of issues based on there <a href = "http://www.sonybiz.ca/pro/lang/en/ca/support/attachment/1237493435790/1237493435817/releasenote-for-media-file-player-v1-6-3-e.pdf">release notes</a> . Any thoughts on what may be causing the issue?</p>Jameshttps://www.mathworks.com/matlabcentral/profile/authors/1950815-jamestag:www.mathworks.com,2005:Question/2669942016-02-07T14:38:38Z2016-02-07T15:07:29Zwhat is the meaning of "order of grey level" in an image?<p>Hello all,
I am new to Image processing so i am trying to get hold of basics. After a lot of search i was able to find how to determine number of grey levels in an image. Now i am stuck with "order of grey levels".
Can any one help in understanding order of grey levels? any help is appreciated.</p>Dinesh Kumar Rhttps://www.mathworks.com/matlabcentral/profile/authors/6696985-dinesh-kumar-rtag:www.mathworks.com,2005:Question/1464212014-08-05T19:26:12Z2016-02-07T15:02:05ZHow do you find a centroid of an image?<p>I am trying to find the centroid of a binary image using region props but I keep receiving the error "Field reference for multiple structure elements that is followed by more reference blocks is an error". Here is my code for finding the centroid:</p><pre> measurements = regionprops(bw, 'Centroid');
hold on;
plot(measurements.Centroid(1), measurements.Centroid(2), 'r+', 'MarkerSize', 120, 'LineWidth', 1);
hold off;
imshow(bw)</pre><p>Any help would be appreciated thank you</p>Rishhttps://www.mathworks.com/matlabcentral/profile/authors/3634331-rishtag:www.mathworks.com,2005:Question/2669962016-02-07T14:51:27Z2016-02-07T14:51:27Zhow to solve -8 error in license file?<p>i installed matlab in my pc and i activated it but the problem is when i run it it says license manager error -8 (license cheak out failed) and i entered troubleshot and i did not get the idea of fixing it</p>haidar alrebhhttps://www.mathworks.com/matlabcentral/profile/authors/5975717-haidar-alrebhtag:www.mathworks.com,2005:Question/2669952016-02-07T14:44:52Z2016-02-07T14:44:52ZI'm trying to make a surface 3d plot of this code. But i keep getting just the figure? <p>Here is my code so far:
theta= 0:2*pi
ra=1
r=0:ra
x=r*cos(theta)
y=r*sin(theta)
z=sqrt(ra^2-x^2-y^2)
surf(x,y,z,'r')
axis equal
title('Spherical')</p>Kevin scotthttps://www.mathworks.com/matlabcentral/profile/authors/7502717-kevin-scotttag:www.mathworks.com,2005:Question/2669742016-02-07T09:42:58Z2016-02-07T14:38:59Zif i have two identical line in graph how can i make them visible for two dat<pre class="language-matlab">x=[ 0.10 0.15 0.18 0.19]
y=[0.11 0.156 0.1888 0.195]
</pre>YOGESHWARI PATELhttps://www.mathworks.com/matlabcentral/profile/authors/7631465-yogeshwari-pateltag:www.mathworks.com,2005:Question/2664082016-02-03T09:59:05Z2016-02-07T14:24:09ZHow to find cell match in matlab table and exctract data from table<p>Let's say I have a table. And a cell array, with</p><pre class="language-matlab">names = {'Smith, 'Williams}
</pre><pre> Gender Age Smoker BloodPressure
______ ___ ______ _______________</pre><pre> Smith Male 38 true 124 93
Johnson Male 43 false 109 77
Williams Female 38 false 125 83
Jones Female 40 false 117 75
Brown Female 49 false 122 80</pre><p>Now I would like to find the names match from the table and extract the values of other variables Gender, Age, Smoker, BloodPressure. I tried to use isfield, but this does not work and I am running out of ideas. I was plaaning to pull the row number and then all the column according to the row.</p><pre> if isfield(Table,Names);</pre><pre> end</pre><p>I tried finding</p>Gert Lõhmushttps://www.mathworks.com/matlabcentral/profile/authors/6615705-gert-lohmustag:www.mathworks.com,2005:Question/2669912016-02-07T14:01:06Z2016-02-07T14:22:54ZIs it possible to use Bluetooth with Matlab + Raspberry Pi?<p>Hello there :)
I'm currently searching for possibilities to connect an raspi wireless to Matlab/Simulink.
The aim is to read data from a sensor connected to the raspi.
I saw tutorials describing how to do that via wifi. Would it also work with a usb bluetooth dongle connected to the raspi? Couldn't find anything about that on the internet, so I would be really thankful for any feedback.</p><p>Kind regards :)</p>Student88https://www.mathworks.com/matlabcentral/profile/authors/7631997-student88tag:www.mathworks.com,2005:Question/2669782016-02-07T10:08:01Z2016-02-07T14:09:26ZHow to convert all jpg images to gray and then save them in different file <p>I have a set of images(jpg) in a file and I want to create a for loop to go through all of them and convert them to a gray scale images and save them into the same directory.
I am using this code:</p><pre class="language-matlab">contents = dir('*.jpg') % or whatever the filename extension is
for i = 1:numel(contents)
filename = contents(i).name;
% Open the file specified in filename, do your processing...
[path name] = rgb2gray(filename);
out_filename = sprintf('%s.jpg', name);
% write jpg output to 'out_filename'
fprintf(1, 'Writing %s\n', out_filename);
end
</pre><p>I actually want to know how I can do two things. First is gray all the images and store them into my directory, second is to add my gray images into .mat file
I will appreciate your help.</p>iris the manhttps://www.mathworks.com/matlabcentral/profile/authors/7325440-iris-the-mantag:www.mathworks.com,2005:Question/2669922016-02-07T14:09:23Z2016-02-07T14:09:23ZA program that while tell you when your next even birthday is (10, 20, 30, 40, etc..)<p>So basically the program is supposed to ask for these two things:</p><p>Whats your name?</p><p>Which year are you born?</p><p>Then it will tell you the year that you turn an even number. So for instance:</p><p>My name is Andreas</p><p>I'm born 1974</p><p>Your next even birthday is:</p><p>2024</p><p>Also if you turn an even number THIS year, it will congratulate you for it</p><p>What's your name? Andreas</p><p>Which year are you born? 1996</p><p>Congratulations Andreas</p><p>You turn 20 this year.</p><p>This is how i've thought of doing so far:</p><pre class="language-matlab">name=input('Whats your name? ')
year=input('Which year are you born? ')
</pre><pre class="language-matlab">Then i have to write a program which calculates the next time you turn an even number.
However it can't be anything under your current age. Also on the end i'll simply just add:
</pre><pre class="language-matlab">disp('Your next even birthday is: ')
disp('next_even_birthday_year')
</pre>Andreas Larssonhttps://www.mathworks.com/matlabcentral/profile/authors/7619500-andreas-larssontag:www.mathworks.com,2005:Question/2669812016-02-07T10:46:40Z2016-02-07T13:58:57Zcircle plot from (2,N) Matrix<p>I have to plot a circle from a (2,N) matrix where x and y are the coordinates of N points around the circle. Center is (0,3) and theta=pi/2 and phi is 0-2pi.. Radius is 2. Should look like the red circle in the back on the picture</p><img src = "/matlabcentral/answers/uploaded_files/44903/Udklip.JPG">Tina Jensenhttps://www.mathworks.com/matlabcentral/profile/authors/6805870-tina-jensentag:www.mathworks.com,2005:Question/2669902016-02-07T13:56:38Z2016-02-07T13:57:30ZEditing and saving iterations .csv files <p>I am trying to solve an optimization problem. This involves reading in a input.csv input file to another program that goes through some calculations. This input file consists of a few rows of headers (strings and numbers), followed by the data. The data is organized into 4 columns: t, x, y, z. For my purposes, I needed to</p><p>1) Read in the z column from the orginal .csv input file</p><p>2) Iterate on those values.</p><p>3) Edit the original file, and replace the z column.</p><p>4) Save that new edited version of the edited_input.csv file, with a different (iterating) file name. In order for this new edited_input.csv file to work , it needs to contain all the original headers.</p><p>I've figured out steps 1 & 2. I am able to read in the data values from the original .csv file. I have edited the Z column, and saved that array as a new variable. The difficulty I am having is steps 3 & 4. Can someone please point me into the right direction?</p>Robinhttps://www.mathworks.com/matlabcentral/profile/authors/3583831-robintag:www.mathworks.com,2005:Question/2669062016-02-06T18:58:35Z2016-02-07T13:50:47ZSolve function not giving complex solutions<p>Hello.
For some reason <i>solve</i> does not seem to give all the existing solutions to simple equations.
For instance, entering:</p><pre class="language-matlab">syms x
s=solve(x^2+1,x)
</pre><p>I get:</p><p>Warning: Explicit solution could not be found.
> In solve at 179</p><p>s =</p><p>[ empty sym ]</p><p>The two solution I expected to see are of course +i and -i.
I use Matlab R2014A.
Any idea what might be causing this?
Thanks in advance.</p>Yoav Bhttps://www.mathworks.com/matlabcentral/profile/authors/5189144-yoav-btag:www.mathworks.com,2005:Question/2669292016-02-06T22:57:18Z2016-02-07T12:57:43Zhow to optimize neural network thyroid example using genetic algorithm?<p>i'm trying to to optimizing thyroeid example by pattern recognition .</p><p>i wana modify hidden layer size , and i want it from ga function ,</p><p>in default way is : hiddenLayerSize = [4 5 ] ;
net = patternnet(hiddenLayerSize);</p><p>now i want hiddenLayerSize get from ga func , that gaves me minimum and optimal number of neuron .</p><p>is there anywat to omplent this case, tnx and srry for my broken english (</p>kooroshhttps://www.mathworks.com/matlabcentral/profile/authors/5912752-kooroshtag:www.mathworks.com,2005:Question/2669892016-02-07T12:57:25Z2016-02-07T12:57:25Zhow can you find the length of each pixel<p>how can you find the length of each pixel.
Is it different for different size images
help me in this matter</p>narayana reddyhttps://www.mathworks.com/matlabcentral/profile/authors/7363732-narayana-reddytag:www.mathworks.com,2005:Question/2669882016-02-07T12:54:49Z2016-02-07T12:54:49Zhow to flip a segmented image along the major axis<p>I and my friends are doing project on skin cancer detection using image processing, We completed upto segmentation, now we have to find area of non overlapped region of the segmented image.For this we have to flip the image along its major axis andminor axis. so please help us in this matter.</p><p>Advance thanks.</p>narayana reddyhttps://www.mathworks.com/matlabcentral/profile/authors/7363732-narayana-reddytag:www.mathworks.com,2005:Question/2669632016-02-07T07:39:54Z2016-02-07T12:53:09Zwhy we need to convert the uint8 to double<p>double()</p>Sharath Reddyhttps://www.mathworks.com/matlabcentral/profile/authors/7472379-sharath-reddytag:www.mathworks.com,2005:Question/2667892016-02-05T12:35:42Z2016-02-07T12:50:02Zhow to get max, min and avg of a field of a structure in a cell matrix<p>Hi</p><p>I have a cell matrix consisting of 3 columns and 400 rows. Every entry in this matrix is a structure. Lets say three columns are A, B and C. Column A contains same structure StructureA, Column B contains same structure StructureB, Column C contains same structure StructureC in all rows. Let say StructureC contains 4 fields field1, field2, field3 and field4.</p><ol><li>I want to find max and min value of field3 across all rows in column C.</li><li>Then I want to find out which row of ColumnC contains max value and which row contains min value of field3.</li><li>Then I want to find out the average of field3 across all rows in ColumnC.</li><li>Finally I want to get all the rows where field3 equals to some value, such as get all the rows where field3 = 'abc' in ColumnC.</li></ol><p>I have already tried below code to calculate the max value but it did not work</p><pre class="language-matlab">maxField3 = max(Column3{:,3}.field3)
</pre><p>I don't want to use loop for calculating these things. Please help.</p><p>Thanks in advance.</p>Anil Vermahttps://www.mathworks.com/matlabcentral/profile/authors/6858268-anil-vermatag:www.mathworks.com,2005:Question/2669732016-02-07T09:39:06Z2016-02-07T12:46:38Zhow to enter function expoential function with two variable<p>calculate u(x,t)= exp (a*x+b*t)</p>YOGESHWARI PATELhttps://www.mathworks.com/matlabcentral/profile/authors/7631465-yogeshwari-pateltag:www.mathworks.com,2005:Question/2669872016-02-07T12:41:02Z2016-02-07T12:41:02Zhi i have want to do an program to extract number of car bu using template matching on arabic characters <p>using template function</p>walid kamelhttps://www.mathworks.com/matlabcentral/profile/authors/5389260-walid-kameltag:www.mathworks.com,2005:Question/2669842016-02-07T11:31:18Z2016-02-07T12:28:05ZNeed help with my program to calculate the circumference and area of a circle.<pre class="language-matlab">%Function to compute area and circumference of a circle given the radius%
%-----------------------------------------------------------------------%
function[circ, area]= circarea(radius)
circ=2*pi*radius;
area=pi*radius*radius;
fprintf('the circumference of the circle is %f\n',circ);
fprintf('the area of the circle is %f\n', area);
end
%For some reason it is displaying an ans= at end of program%
</pre>Timothy Stateshttps://www.mathworks.com/matlabcentral/profile/authors/7518746-timothy-statestag:www.mathworks.com,2005:Question/2669862016-02-07T12:22:28Z2016-02-07T12:56:04Zhow to plot graph <pre class="language-matlab">RGB =imread('image.jpg');
hsv= rgb2hsv(RGB);
imshow(hsv);
r1=0.1;
r2=0.85;
i= 0:20:240;
s= 0.0:0.2:1.0;
wgray = 1 - bsxfun(@power, s(:), (r1*(255./(i(:).')).^r2) );
P = gradient(wgray,0.2);
Q= gradient(wgray,0.4);
R= gradient(wgray,0.6);
S= gradient(wgray,0.8);
T= gradient(wgray,1.0);
hist(P+Q+R+S+T,i);
</pre><p>I want to plot a graph beween partial derivative of s(whereas s varies from 0.0 to1.0) and i</p><p>Thanks.</p>kalyani ohrihttps://www.mathworks.com/matlabcentral/profile/authors/7544930-kalyani-ohritag:www.mathworks.com,2005:Question/2669572016-02-07T06:03:53Z2016-02-07T12:13:02ZFor Loop Backwards Help<p>This is my code. And these are the values it produces.</p><p>0 5.0000 9.6000 13.8320 17.7254 21.3074 24.6028 27.6346 30.4238 32.9899 35.3507</p><pre> x = zeros(1,10);</pre><pre> for i = 1:10</pre><pre> x(i+1) = x(i) + ((5 - (4/50)*x(i)));</pre><pre> end
display(x)</pre><p>I want the code to now run backwards now. I want to start at the 35.3507 value and run backwards now 10 times. But I cannot seem to make it work. Any help would be great.</p><p>Thanks</p>WhatIsMatlab-https://www.mathworks.com/matlabcentral/profile/authors/7623251-whatismatlabtag:www.mathworks.com,2005:Question/2669852016-02-07T11:55:55Z2016-02-07T11:55:55ZHow to model a magnetic force in sim mechanics<p>Hello,</p><p>I want to model magnetic force between two rigid bodies (electromagnet and piece of steel) in simmechanics. Magnetic force has form F=k(i+a)/(x+b). k,a and b are constants. i is current, x is gap between magnet and piece of steel.</p><p>I looked at inverse square law force in sim mechanics. but it asks only for one constant.</p><p>Please help.</p>budannahttps://www.mathworks.com/matlabcentral/profile/authors/6669224-budannatag:www.mathworks.com,2005:Question/2669482016-02-07T03:24:01Z2016-02-07T11:29:16Zhow to generate hdl code for edge detection block in simulink<img src = "/matlabcentral/answers/uploaded_files/44881/Capture.PNG">GOKUL GKhttps://www.mathworks.com/matlabcentral/profile/authors/7611654-gokul-gktag:www.mathworks.com,2005:Question/2669832016-02-07T11:26:04Z2016-02-07T11:26:04Zhow to clear interface error and assertion error<img src = "/matlabcentral/answers/uploaded_files/44904/interface%20erro.PNG">GOKUL GKhttps://www.mathworks.com/matlabcentral/profile/authors/7611654-gokul-gktag:www.mathworks.com,2005:Question/2669602016-02-07T06:55:28Z2016-02-07T11:03:43Zslow data analysis on mac as compared to window with R2015a platform?<p>I am using a code for data analysis of microscopy data. The code runs fine of windows computer (2.5 GhZ, 4GB). But when I run it on my macbook pro (2.9 GhZ, 16GBDDR3) laptop it is runs 30-40 times slower. the platform I am using if R2015a.</p>Pranav Purihttps://www.mathworks.com/matlabcentral/profile/authors/6843926-pranav-puritag:www.mathworks.com,2005:Question/2669792016-02-07T10:26:47Z2016-02-07T13:29:57Zhow to make an image over some background picture?<p>Hello,</p><p>I am gradually drawing an image on axes (creating the effect of image "slides in"), using</p><pre> set(gca,'xlim',[0 120*fps],'ylim',[0 25],'nextplot','add','ydir','reverse');</pre><p>Question is, the default background is a a simple white plane. How can I change it to my own picture?</p><p>I mean, if I place my picture on that axes, then the xlim and ylim becomes the size of the picture and I loose the settings I've made in an earlier command.</p><p>THANK YOU</p>Mark Golberghttps://www.mathworks.com/matlabcentral/profile/authors/6665405-mark-golbergtag:www.mathworks.com,2005:Question/2669772016-02-07T10:07:57Z2016-02-07T10:07:57Zi need an activation key for Host ID: DISK_SERIAL_NUM=340b53b4 Release: R2009b<p>i need an activation key for Host ID: DISK_SERIAL_NUM=340b53b4 Release: R2009b</p>ala belhadjhttps://www.mathworks.com/matlabcentral/profile/authors/7631501-ala-belhadjtag:www.mathworks.com,2005:Question/2661622016-02-02T03:56:22Z2016-02-07T10:06:33ZUnsucceed to use a valid activation key.<p>The error:</p><p>"The license number or activation key did not match any existing license number or activation key. Please try again."</p><p>tried to activate a valid license key, but keeping to receive an error message. maybe because on the account i put "student" by mistake, instead of "academic" use?</p><p>Lev Gurvits < <a href = "mailto:Lev.Gurvits@mail.citytech.cuny.edu">Lev.Gurvits@mail.citytech.cuny.edu</a> ></p>Lev Gurvitshttps://www.mathworks.com/matlabcentral/profile/authors/7598444-lev-gurvitstag:www.mathworks.com,2005:Question/2669762016-02-07T10:04:07Z2016-02-07T10:04:07ZSampling big epinions data-set<p>I'm working on recommendation system to solve cold start problem in matlab, first of all I get extended version of epinions data-set, its really big data-set, about 13 millions rating, 120000 users and 755000 items, my problem is because working on this data-set needs high memory, I cant do anything on it, I asked my supervisor and he said I should use sampling, but how can I sampling 13 million rows ? Every statement that I perform on this dataset takes about one week to be finished, what can I do now ? Thank every one</p>shayan asadpoorhttps://www.mathworks.com/matlabcentral/profile/authors/5110981-shayan-asadpoortag:www.mathworks.com,2005:Question/2669622016-02-07T07:23:15Z2016-02-07T10:01:19Zhow reduce time of numerical integral?<p>my Numerical Solution of Integral takes a lot of time. While I need accuracy to four decimal places for answer . How do I reduce the decimal precision of integrator answer to reduce time ?
for example:</p><p>A=double(int(f(x),a,b));</p>Brede robersonhttps://www.mathworks.com/matlabcentral/profile/authors/7569427-brede-robersontag:www.mathworks.com,2005:Question/2669722016-02-07T09:37:36Z2016-02-07T09:37:36Zhow to select the least used gpuDevice?<p>I run multiple matlab processes on several gpus on one machine. Is there a way to select the least used gpuDevice based on the current load?
Thanks!</p>Jack Leehttps://www.mathworks.com/matlabcentral/profile/authors/3353667-jack-leetag:www.mathworks.com,2005:Question/2669712016-02-07T09:34:43Z2016-02-07T09:34:43ZInteger-constrained GA default genetic operator functions<p>Hi there, I am using ga() to run an optimization problem where I have 8 variables, 6 of which have integer constrains.</p><p>Everything's ok, but I noticed that Matlab overwrites the Selection, Mutation and Crossover functions (and that is reported in <a href = "http://it.mathworks.com/help/gads/mixed-integer-optimization.html#bs1cifg)">http://it.mathworks.com/help/gads/mixed-integer-optimization.html#bs1cifg)</a>.</p><p>Although I was not able to find <b>which</b> are the default options in such cases. It's ok for me if Matlab overwrites custom functions for integer problems, but at least I'd like to know what its default behaviour is.</p><p>Does anybody know the default Selection, Mutation, Crossover functions in integer (or mixed-integer) problems?</p><p>King Regards</p>GothicPurplehttps://www.mathworks.com/matlabcentral/profile/authors/5959553-gothicpurpletag:www.mathworks.com,2005:Question/2668722016-02-06T08:34:53Z2016-02-07T09:30:53ZCounts in bins for 2D histogrsm using nested for loop ?<pre> I am trying to write extend 1D histogram code to 2D histogram, after creating bins using mesh grid
how to count if data point x,y is in the bin?
%1D code
A = [1 ;1 ;2 ;2 ;2 ;4 ;4 ;4 ;4 ;4 ;4 ;5 ;5 ;6 ;1 ];
A_u = unique(A);
z = zeros(size(A_u));
for i = 1:length(A_u)
z(i) = sum(A_u(i)==A); %Counts the frequency, need something similar for 2D
end
z = [A_u, z];</pre><pre> %2D code</pre><pre> x = [1,2,5,6,7,3,4,9,5,2,8]';
y = [1,1,3,6,4,3,6,5,8,7,4]';
xy = [x,y];
mn_x = min(x);
mx_x = max(x);
mn_y = min(y);
mx_y = max(y);
x_rng = linspace(mn_x,mx_x,3);
y_rng = linspace(mx_y,mn_y,3);
[p,q] = meshgrid(x_rng,y_rng);
z = zeros(size(p,1),size(q,2));
for i = 1:size(p,1)
for j = 1:size(q,2)
%Need help here to decide criteria, like 1D histogram,
problem is I cant access all elements of xy as counters for loops goes
through rows and columns of p and q.
z(i,j) = sum( (p(i) < xy(:,1) < p(i+1,:)) & (q(j) < xy(:,2) < q(j+1,:)) );
end
end</pre>dipak sanaphttps://www.mathworks.com/matlabcentral/profile/authors/7152486-dipak-sanaptag:www.mathworks.com,2005:Question/2665582016-02-04T05:28:40Z2016-02-07T09:22:50ZHow to compile a simulink model for 3d surface plot, which can find z for user defined (x,y)<p>I have a 3-d table, lets assume x=first row, y = first column and z is matrix for x and y.
I could use curve fit in matlab to get the surface plot and linear polynomial equation.
But i want to compile a simulink model which has to find the z value for a given x,y looking up the table or through the equation.</p>RANJITH REDDY KALLURIhttps://www.mathworks.com/matlabcentral/profile/authors/7505368-ranjith-reddy-kalluritag:www.mathworks.com,2005:Question/2669692016-02-07T08:21:07Z2016-02-07T08:45:09ZCombine two matrices (every other column)<p>I have two matrices A and B which are for example</p><p>A=[1 2 3;4 5 6; 7 8 9] and B=[10 11 12; 13 14 15; 16 17 18]</p><p>And I would like to combine these matrices so that every other column is from A and every other is from B. So the answer should be matrix:</p><p>[1 10 2 11 3 12; 4 13 5 14 6 15; 7 16 8 17 9 18]</p><p>Of course my matrices are not only 3x3 matrices but n x n matrices.</p><p>My history with Matlab is so short that I don't figure out if that is even possible to do?</p>Snowfallhttps://www.mathworks.com/matlabcentral/profile/authors/7587828-snowfalltag:www.mathworks.com,2005:Question/2669702016-02-07T08:34:02Z2016-02-07T08:34:02ZWhile using parfor the cpu usage drops by 1 core every something iterations<p>While using <b>parfor</b> loop for a long time everything seems to work fine loading all 8 cores of my CPU but after sometime Matlab releases cores one by one with some time in between exponentially increasing time working on the loop.</p><p>Triple <b>for</b> loop I use:</p><pre class="language-matlab">N = size(A,1); % around 5000
</pre><pre class="language-matlab">parfor i = 1:size(A,1)
</pre><pre> for j = i+1:N</pre><pre> for k = j+1:N</pre><pre> sumMatrix = [sumMatrix; [(A(i) + A(j) + A(k)) A(i) A(j) A(k)]];</pre><pre> end
end
end</pre>Stepan Ulyaninhttps://www.mathworks.com/matlabcentral/profile/authors/7597472-stepan-ulyanintag:www.mathworks.com,2005:Question/2669662016-02-07T08:06:10Z2016-02-07T08:29:16ZI can not figure out what's wrong with my code<p>t = -2:0.01:4; x = zeros(size(t));
x(find(t=-1 & t=1)) = 0.5*t+0.5; x(find(t=1 & t=3)) = -0.5*t+1.5; y = dirac(2); z = conv(x,y); plot(t,z);</p><p>I get this error message for line 3: "In an assignment A(I) = B, the number of elements in B and I must be the same."</p>Saurav Goyalhttps://www.mathworks.com/matlabcentral/profile/authors/7506794-saurav-goyaltag:www.mathworks.com,2005:Question/2669672016-02-07T08:08:05Z2016-02-07T08:27:34Zproblem in Matlab!<p>hello guys. I did a problem and the matlab runs and it works. but matlab did the opposite. I want the x_r to be 2.0239 and x_l to be 1.9866, but matlab did the opposite.</p><p>this was the question and the given info.</p><img src = "/matlabcentral/answers/uploaded_files/44900/Screen%20Shot%202016-02-06%20at%209.05.59%20PM.png"><p>this is the solution that I followed as you see in the third iteration x_r must be 2.0239 and x_l must be 1.9866</p><img src = "/matlabcentral/answers/uploaded_files/44901/Screen%20Shot%202016-02-06%20at%209.06.24%20PM.png"><p>those were my codes</p><img src = "/matlabcentral/answers/uploaded_files/44902/Screen%20Shot%202016-02-07%20at%201.51.22%20AM.png"><p>as you see in the workspace the result is the opposite from the actual solution so what was the mistake and how to fix it?</p>abdulaziz almutairihttps://www.mathworks.com/matlabcentral/profile/authors/5856324-abdulaziz-almutairitag:www.mathworks.com,2005:Question/2669442016-02-07T02:42:27Z2016-02-07T08:08:44Zhow to find mid point between two centroids of objects?<p>hello friends,</p><p>I want to find mid points between two centroids of objects.</p><p>I have used this code :</p><pre class="language-matlab">for n=1:Ne
[r,c] = find(L==n);
s = regionprops(L, 'centroid');
centroids = cat(1, s.Centroid);
imshow(image);
hold on
plot(centroids(:,1), centroids(:,2), 'b*');
</pre><p>I found the result as</p><img src = "/matlabcentral/answers/uploaded_files/44883/Capture3.JPG"><p>This code gives the centroids from the corner points too. which are unnecessary. how to remove them?</p><p>I would be thankful if someone helps me to find mid point between two two centroids of objects by finding the distance between them or else.</p><p>thanks in advance.</p>Rujalhttps://www.mathworks.com/matlabcentral/profile/authors/5773756-rujaltag:www.mathworks.com,2005:Question/2657082016-01-29T07:29:42Z2016-02-07T08:03:19ZReturn rows (not just row number)<p>Hello</p><p>I would be happy if anyone could help me with following problem.</p><p>I have an text (or excel) file which is in the following form (or course in reality it is in larger form but anyway the basic idea is):</p><img src = "/matlabcentral/answers/uploaded_files/44297/table1.JPG"><p>And now I would like to find all the 'Cat'-rows and return that row and the following row.</p><p>So in the end my answer should be</p><img src = "/matlabcentral/answers/uploaded_files/44298/table2.JPG"><p>And I don't even know how to begin. Could anyone help?</p>Snowfallhttps://www.mathworks.com/matlabcentral/profile/authors/7587828-snowfalltag:www.mathworks.com,2005:Question/2669652016-02-07T08:02:33Z2016-02-07T08:02:33Zhow can i only show the first bbox drew by blobAnalysis<p>i'm working with motion detection using blobAnalysis to draw a rectangle on each object detected as mobile, here i want to show only one blob based on some criteria...how can i do it please, i really need help
here there is the code i used:</p><pre class="language-matlab">foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 10);
for i= 1:1000;
foreground = step(foregroundDetector, frame);
se = strel('square', 3);
filteredForeground = imopen(foreground, se);
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 250, 'MaximumBlobArea', 1000);
bbox = step(blobAnalysis, filteredForeground);
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
numObjects = size(bbox, 1);
result = insertText(result, [10 10], numObjects, 'BoxOpacity', 1, ...
'FontSize', 14);
imagesc(result);
drawnow;
end
</pre><p>i think i should modify something on this instruction:</p><pre class="language-matlab">Result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
</pre><p>is it right to write 'bbox(1,:)' for example?</p>bay remhttps://www.mathworks.com/matlabcentral/profile/authors/7464625-bay-remtag:www.mathworks.com,2005:Question/2669642016-02-07T07:48:57Z2016-02-07T07:48:57ZHow to add slider to figure ? manual thresholding<p>Hi</p><p>I have a an image that I want to add a ui to threshold manually. code below</p><pre class="language-matlab">im = imread('image.png);
</pre><pre class="language-matlab">for i = 1:rows
for j = 1:cols
</pre><pre> if(im(i,j) < threshold)
out(i,j) = 0;
elseif(im(i,j) > threshold)
out(i,j) = 1;
end
end
end</pre><pre class="language-matlab">imshow(out);
</pre><p>how do I add a slider that can manually change the threshold in the figure ? help thanks</p>Haziq Razalihttps://www.mathworks.com/matlabcentral/profile/authors/6016970-haziq-razalitag:www.mathworks.com,2005:Question/2668452016-02-05T22:18:54Z2016-02-07T07:42:00ZHow to create a matrix using nested for loops<pre class="language-matlab">t=linspace(0,2,100);
x=linspace(0,1,50);
n=linspace(1,5,5000);
sum=0;
</pre><pre> for x1=0:50</pre><pre> for t1=0:100</pre><pre> for n1=1:5000</pre><pre> sum(x1,t1,n1)= sum(x1,t1,n1) + x.^n/factorial(n)+ t.^n/(50*factorial(n));</pre><pre> end </pre><pre> end
end </pre><p>This is my code. Essential I'm trying to create a matrix of the solutions to the sum so I can later use the functions surf. However once I run the code I get "Subscript indices must either be real positive integers or logicals." What I'm doing wrong and can be done to fix my code? Thank you.</p>jesus escarenohttps://www.mathworks.com/matlabcentral/profile/authors/4703508-jesus-escarenotag:www.mathworks.com,2005:Question/2660562016-02-01T11:24:33Z2016-02-07T07:20:49ZWhere is the problem in my neural network script? I cannot update the weights and biases after training.<p>I am trying to design a custom neural network model to solve a problem. Here is the script:</p><pre class="language-matlab">net= network;
net.numInputs = 1;
net.inputs{1}.size = 1;
net.numLayers = 2;
net.layers{1}.size = 5;
net.layers{2}.size = 1;
net.inputConnect(1,1) = 1;
net.inputConnect(2,1) = 0;
net.biasConnect(1) = 1;
net.biasConnect(2) = 0;
net.layerConnect(2,1) = 1;
net.layers{1}.transferFcn = 'logsig';
net.layers{2}.transferFcn = 'purelin';
net.inputWeights{1,1}.initFcn = 'initzero';
net.inputWeights{1,1}.learn = 1;
net.inputWeights{1,1}.learnFcn = 'learngd';
net.layerWeights{2,1}.initFcn = 'initzero';
net.layerWeights{2,1}.learn = 1;
net.layerWeights{2,1}.learnFcn = 'learngd';
net.biases{1}.initFcn = 'initzero';
net.biases{1}.learn = 1;
net.biases{1}.learnFcn = 'learngd';
net.outputConnect = [0 1];
net.initFcn = 'initlay';
net.trainFcn = 'traingd';
net.performFcn = 'sse';
</pre><p>And here is the the diagram for the neural network:</p><img src = "/matlabcentral/answers/uploaded_files/44452/neural%20network.png"><p>Before I trained the network, I typed</p><pre class="language-matlab">net.IW{1}; net.b{1}; net.LW{2,1}
</pre><p>in the command window and checked that the values were all initially zero. However, when I trained the network, say</p><pre class="language-matlab">train(net, 0.1, 1)
</pre><p>Even though there was a pop-up window showing the process,</p><img src = "/matlabcentral/answers/uploaded_files/44457/neural%20network%202.png"><p>it seemed that the weights and biases did not update as I typed net.IW{1}, net.b{1} and net.LW{2,1}. Also, the output was always zero, no matter what value I input using</p><pre class="language-matlab">sim()
</pre><p>Could anyone help me to indicate the problem in the script or the procedure in training? Thank you.</p>Eric Leonghttps://www.mathworks.com/matlabcentral/profile/authors/7599751-eric-leong