<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.3.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: MATLAB Basics video: Storing data in a matrix from a loop</title>
	<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/</link>
	<description>&#60;a href="http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=969735&#38;objectType=author"&#62;Bob&#60;/a&#62;, &#60;a href="http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=1093599&#38;objectType=author"&#62;Brett&#60;/a&#62; &#38; &#60;a href="http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectId=1094142&#38;objectType=author"&#62;Jiro&#60;/a&#62; share favorite user-contributed submissions from the File Exchange.</description>
	<pubDate>Mon, 23 Nov 2009 00:47:54 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.3.1</generator>
		<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13294</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Tue, 10 Nov 2009 21:15:25 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13294</guid>
		<description>@Ashok

First, please do not use variable names that are MATLAB commands (std and mean).

Second, p(j) should be p(i)

Third, look at this line of code:

p(j) = mean(i) + std(i)*randn(100,1);

This says

The jth element of the matrix p should be the sum of a scalar(mean(i)) and a vector(std(i)*randn(100,1)).

This does not make sense to put a vector into the element of a matrix.

Fourth, usually asking for user input like that is slow, and causes trouble since people can type anything.

Enjoy,
Doug</description>
		<content:encoded><![CDATA[<p>@Ashok</p>
<p>First, please do not use variable names that are MATLAB commands (std and mean).</p>
<p>Second, p(j) should be p(i)</p>
<p>Third, look at this line of code:</p>
<p>p(j) = mean(i) + std(i)*randn(100,1);</p>
<p>This says</p>
<p>The jth element of the matrix p should be the sum of a scalar(mean(i)) and a vector(std(i)*randn(100,1)).</p>
<p>This does not make sense to put a vector into the element of a matrix.</p>
<p>Fourth, usually asking for user input like that is slow, and causes trouble since people can type anything.</p>
<p>Enjoy,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ashok</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13287</link>
		<dc:creator>Ashok</dc:creator>
		<pubDate>Thu, 05 Nov 2009 08:58:33 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13287</guid>
		<description>how to store 10 or more random number in a loop a loop
for i = 1:n
    mean(i) = input('enter the mean value of distribution = ')
    std(i) = input('enter the std value of distribution = ')
      p(j) = mean(i) + std(i)*randn(100,1);
end
how to make it work becoz there is string mismatch. Please tell me</description>
		<content:encoded><![CDATA[<p>how to store 10 or more random number in a loop a loop<br />
for i = 1:n<br />
    mean(i) = input(&#8217;enter the mean value of distribution = &#8216;)<br />
    std(i) = input(&#8217;enter the std value of distribution = &#8216;)<br />
      p(j) = mean(i) + std(i)*randn(100,1);<br />
end<br />
how to make it work becoz there is string mismatch. Please tell me</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13280</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Tue, 27 Oct 2009 14:09:09 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13280</guid>
		<description>@Leo,

Here is the "English version" of that code.


"vec = []" makes an empty variable named 'vec'.

"vec = [vec val]" makes the variable 'vec' equal to the variable 'vec' and variable 'val' in a row.  (This would fail if 'vec' was not already defined, thus the above line.)  This was done in a loop, so the empty 'vec' is there for the first iteration really.

-Doug</description>
		<content:encoded><![CDATA[<p>@Leo,</p>
<p>Here is the &#8220;English version&#8221; of that code.</p>
<p>&#8220;vec = []&#8221; makes an empty variable named &#8216;vec&#8217;.</p>
<p>&#8220;vec = [vec val]&#8221; makes the variable &#8216;vec&#8217; equal to the variable &#8216;vec&#8217; and variable &#8216;val&#8217; in a row.  (This would fail if &#8216;vec&#8217; was not already defined, thus the above line.)  This was done in a loop, so the empty &#8216;vec&#8217; is there for the first iteration really.</p>
<p>-Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: leo</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13279</link>
		<dc:creator>leo</dc:creator>
		<pubDate>Tue, 27 Oct 2009 07:10:10 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13279</guid>
		<description>Dear Doug 
I have a question in your code 'October 9th, 2009 at 13:53'

vec = [];
vec = [vec val];

I don't know the meaning of these statements.
I just started the matlab.
So, i don't know well about matlab.
Could you tell me the meaning?</description>
		<content:encoded><![CDATA[<p>Dear Doug<br />
I have a question in your code &#8216;October 9th, 2009 at 13:53&#8242;</p>
<p>vec = [];<br />
vec = [vec val];</p>
<p>I don&#8217;t know the meaning of these statements.<br />
I just started the matlab.<br />
So, i don&#8217;t know well about matlab.<br />
Could you tell me the meaning?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13274</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Mon, 19 Oct 2009 18:08:32 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13274</guid>
		<description>@michael,

Without your code, I can not debug this.  Please post the shortest version of your code that shows the behavior.

Dou</description>
		<content:encoded><![CDATA[<p>@michael,</p>
<p>Without your code, I can not debug this.  Please post the shortest version of your code that shows the behavior.</p>
<p>Dou</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13267</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Thu, 15 Oct 2009 21:28:56 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13267</guid>
		<description>Hi, I think my problem is simple but its been bugging me. Basically I want to save each step in the loop as a single column vector.  Inside the loop is a matrix times a column vector which should result in a colunm vector. In the end I will build a matrix from my collection of vectors i get from the loop. How can I get the loop to save each iteration as a column vector.  thanks alot  
&lt;A&gt;
I'm getting this error message below
???  In an assignment  A(I) = B, the number of elements in B and  I must be the same.</description>
		<content:encoded><![CDATA[<p>Hi, I think my problem is simple but its been bugging me. Basically I want to save each step in the loop as a single column vector.  Inside the loop is a matrix times a column vector which should result in a colunm vector. In the end I will build a matrix from my collection of vectors i get from the loop. How can I get the loop to save each iteration as a column vector.  thanks alot<br />
<a><br />
I&#8217;m getting this error message below<br />
???  In an assignment  A(I) = B, the number of elements in B and  I must be the same.</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13264</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Fri, 09 Oct 2009 17:00:37 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13264</guid>
		<description>Steve,

I can see your point.  I think this comes down to "religious issues"  I started with something like yours, but I did not like the repeated "val &gt;= 0" statement.  I can see that my code obscures the exit condition though.

Thanks for contributing!
Doug</description>
		<content:encoded><![CDATA[<p>Steve,</p>
<p>I can see your point.  I think this comes down to &#8220;religious issues&#8221;  I started with something like yours, but I did not like the repeated &#8220;val >= 0&#8243; statement.  I can see that my code obscures the exit condition though.</p>
<p>Thanks for contributing!<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve L</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13263</link>
		<dc:creator>Steve L</dc:creator>
		<pubDate>Fri, 09 Oct 2009 14:57:01 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13263</guid>
		<description>Normally in a loop I'd preallocate the vector into which I'm going to insert data; however, in this case the limiting factor on the speed of the loop is not going to be reallocating the memory for the growing vector but waiting for the user to enter the value so it's not as big a concern.  I would do things slightly differently from Doug's answer, to avoid the need for the infinite WHILE loop and the BREAK:
&lt;pre class="code"&gt;
val = Inf;
vec = [];
while val &#62;= 0
    val = input('Next value (negative number to quit): ');
    if val &#62;= 0
        vec = [vec val];
    end
end
&lt;/pre&gt;
or, slightly sneakier:
&lt;pre class="code"&gt;
val = Inf;
vec = [];
while val &#62;= 0
    val = input('Next value (negative number to quit): ');
    vec = [vec val];
end
vec(end) = []; % Delete the negative value at the end of vec
&lt;/pre&gt;
I find it easier to read the code if condition is in the WHILE statement rather than somewhere inside the loop, but that's just my personal preference.</description>
		<content:encoded><![CDATA[<p>Normally in a loop I&#8217;d preallocate the vector into which I&#8217;m going to insert data; however, in this case the limiting factor on the speed of the loop is not going to be reallocating the memory for the growing vector but waiting for the user to enter the value so it&#8217;s not as big a concern.  I would do things slightly differently from Doug&#8217;s answer, to avoid the need for the infinite WHILE loop and the BREAK:</p>
<pre class="code">
val = Inf;
vec = [];
while val &gt;= 0
    val = input('Next value (negative number to quit): ');
    if val &gt;= 0
        vec = [vec val];
    end
end
</pre>
<p>or, slightly sneakier:</p>
<pre class="code">
val = Inf;
vec = [];
while val &gt;= 0
    val = input('Next value (negative number to quit): ');
    vec = [vec val];
end
vec(end) = []; % Delete the negative value at the end of vec
</pre>
<p>I find it easier to read the code if condition is in the WHILE statement rather than somewhere inside the loop, but that&#8217;s just my personal preference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13262</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Fri, 09 Oct 2009 13:53:54 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13262</guid>
		<description>&lt;pre class="code"&gt;

vec = [];

while 1
    val = input('Next value (negative to quit): ');
    if val &gt;= 0
        vec = [vec val];
    else
        break
    end
end

&lt;/pre&gt;

Enjoy,
Doug</description>
		<content:encoded><![CDATA[<pre class="code">

vec = [];

while 1
    val = input('Next value (negative to quit): ');
    if val >= 0
        vec = [vec val];
    else
        break
    end
end
</pre>
<p>Enjoy,<br />
Doug</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eta</title>
		<link>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13261</link>
		<dc:creator>Eta</dc:creator>
		<pubDate>Thu, 08 Oct 2009 16:06:22 +0000</pubDate>
		<guid>http://blogs.mathworks.com/pick/2007/08/20/matlab-basics-video/#comment-13261</guid>
		<description>i am trying to create function that a while loop that prompts the user for a number. if the number is 0 or more, the value is stored in a vector and the user is prompted to  enter another number and so on but if the number is negative, the function stops and returns the vector of the previously stored numbers.
this is my code but i know i am doing something wrong in the section where i store the value in a vector and when displaying this vector.

% x is input data value
function [] = whileInput()
x = input('Please enter a value: ');
while x &#62;= 0
    % store value in vector
    vector(x) = 1:length(x);
    % ask for new value
    x = input('Please enter your next value: ');
end
  if  x &#60; 0
      disp(vector);
  else
  end

please help. thank you</description>
		<content:encoded><![CDATA[<p>i am trying to create function that a while loop that prompts the user for a number. if the number is 0 or more, the value is stored in a vector and the user is prompted to  enter another number and so on but if the number is negative, the function stops and returns the vector of the previously stored numbers.<br />
this is my code but i know i am doing something wrong in the section where i store the value in a vector and when displaying this vector.</p>
<p>% x is input data value<br />
function [] = whileInput()<br />
x = input(&#8217;Please enter a value: &#8216;);<br />
while x &gt;= 0<br />
    % store value in vector<br />
    vector(x) = 1:length(x);<br />
    % ask for new value<br />
    x = input(&#8217;Please enter your next value: &#8216;);<br />
end<br />
  if  x &lt; 0<br />
      disp(vector);<br />
  else<br />
  end</p>
<p>please help. thank you</p>
]]></content:encoded>
	</item>
</channel>
</rss>
