http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607
MATLAB Central Newsreader  Converting Matrix Dimensions?
Feed for thread: Converting Matrix Dimensions?
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Sat, 17 Jan 2009 21:54:02 +0000
Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622291
Jens Doe
I have a problem in my class and the data is given in a 20x4 data file. It asks to find the mean, median and other things. However we're not allowed to use the mean function to find the mean. I have an idea that by converting the matrix into a 1x80 and then adding the sum and dividing by 80, I'll get the answer. But how do you convert the matrix from 20x4 to 1x80? Thanks

Sun, 18 Jan 2009 01:00:04 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622306
Sadik
"Jens Doe" <jerald29@hotmail.com> wrote in message <gktk1p$7ak$1@fred.mathworks.com>...<br>
> I have a problem in my class and the data is given in a 20x4 data file. It asks to find the mean, median and other things. However we're not allowed to use the mean function to find the mean. I have an idea that by converting the matrix into a 1x80 and then adding the sum and dividing by 80, I'll get the answer. But how do you convert the matrix from 20x4 to 1x80? Thanks<br>
<br>
Hello Jens,<br>
<br>
Let us call your data matrix as dataMatrix. Then, the following will give the 1x80 version of your data:<br>
<br>
dataMatrix(:).'<br>
<br>
Please note the transpose opearation (.'). I wrote the apostrophe with a period in front in case your data is complex. However, if it is real, then dataMatrix(:)' gives the same result [that is, with only an apostrophe.].

Sun, 18 Jan 2009 15:57:01 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622352
Jos
"Jens Doe" <jerald29@hotmail.com> wrote in message <gktk1p$7ak$1@fred.mathworks.com>...<br>
> I have a problem in my class and the data is given in a 20x4 data file. It asks to find the mean, median and other things. However we're not allowed to use the mean function to find the mean. I have an idea that by converting the matrix into a 1x80 and then adding the sum and dividing by 80, I'll get the answer. But how do you convert the matrix from 20x4 to 1x80? Thanks<br>
<br>
In general, the function RESHAPE is designed to, well, reshape an array. If you want to end up with a vector (Nby1 or 1byN shapes) use reshape with an empty argument, or you can use the colon operation (followed by a transpose)<br>
<br>
Example: reshape(A,[],1) will reshape an array A into a column vector<br>
<br>
hth<br>
Jos

Mon, 19 Jan 2009 00:26:01 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622404
Jens Doe
Thank you for your help so far. However, I can not get either of these two codes to work. I'll post my code for both<br>
<br>
using (:).'<br>
<br>
numbers=input('What data file would you like to load? ','s')<br>
<br>
load(numbers)<br>
<br>
numbers(:).'<br>
<br>
allnumbers=numbers(:).'<br>
<br>
length(allnumbers)<br>
<br>
l=length(allnumbers)<br>
<br>
disp(['Length of data ' num2str(l)])<br>
<br>
sum(allnumbers)<br>
<br>
s=sum(allnumbers)<br>
<br>
mean=s/l<br>
<br>
disp(['Your mean is ' num2str(mean)])<br>
<br>
Two things that are wrong with this. It says that the length of the data is 10 instead of 80 and it says the sum is 915 instead of 6477. <br>
<br>
<br>
Then using the reshape(A,[],1)<br>
<br>
clear<br>
clc<br>
<br>
data=input('What file would you like to load? ','s')<br>
<br>
load(data)<br>
<br>
a=reshape(data,[],1)<br>
<br>
disp(['Length of data ' num2str(a)])<br>
<br>
<br>
With this I'm getting an error of <br>
??? Error using ==> horzcat<br>
CAT arguments dimensions are not consistent.<br>
<br>
I'm not too sure whats wrong with either.

Mon, 19 Jan 2009 00:43:01 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622405
Sadik
"Jens Doe" <jerald29@hotmail.com> wrote in message <gl0hap$83m$1@fred.mathworks.com>...<br>
> Thank you for your help so far. However, I can not get either of these two codes to work. I'll post my code for both<br>
> <br>
> using (:).'<br>
> <br>
> numbers=input('What data file would you like to load? ','s')<br>
> <br>
> load(numbers)<br>
> <br>
> numbers(:).'<br>
> <br>
> allnumbers=numbers(:).'<br>
> <br>
> length(allnumbers)<br>
> <br>
> l=length(allnumbers)<br>
> <br>
> disp(['Length of data ' num2str(l)])<br>
> <br>
> sum(allnumbers)<br>
> <br>
> s=sum(allnumbers)<br>
> <br>
> mean=s/l<br>
> <br>
> disp(['Your mean is ' num2str(mean)])<br>
> <br>
> Two things that are wrong with this. It says that the length of the data is 10 instead of 80 and it says the sum is 915 instead of 6477. <br>
> <br>
> <br>
> Then using the reshape(A,[],1)<br>
> <br>
> clear<br>
> clc<br>
> <br>
> data=input('What file would you like to load? ','s')<br>
> <br>
> load(data)<br>
> <br>
> a=reshape(data,[],1)<br>
> <br>
> disp(['Length of data ' num2str(a)])<br>
> <br>
> <br>
> With this I'm getting an error of <br>
> ??? Error using ==> horzcat<br>
> CAT arguments dimensions are not consistent.<br>
> <br>
> I'm not too sure whats wrong with either.<br>
<br>
Hello Jens,<br>
<br>
For the first method, please make sure that you are loading the correct file. It seems that you are not loading the original data which is 20x4. <br>
<br>
For the second method, yes, it will give error. This is because, given that it will load the same thing with 10 elements, your vector a will be 10x1 and you would not be able to concatenate a 10element vector with a row string. This should help:<br>
<br>
disp(['Length of data ' num2str(length(a))])<br>
<br>
Hope this helps.

Mon, 19 Jan 2009 00:47:01 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622406
Matt Fig
There is a problem here. What you are assigning to the variable 'numbers' is not the data stored in the file, but the name of the file.<br>
If your file name is pizza, then the variable 'numbers' holds the character string 'pizza' which you are then performing your analysis on. You need to address the variables stored in the data file, not the name of the file.<br>
<br>
<br>
<br>
<br>
G^=EFe;M:WGGLWE99?WDGGWLHL@%=9F1QWMW^Wq9GA@w:=N;9WC9HA9D=@

Mon, 19 Jan 2009 01:22:02 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622408
Jens
"Matt Fig" <spamanon@yahoo.com> wrote in message <gl0ii5$ru9$1@fred.mathworks.com>...<br>
> There is a problem here. What you are assigning to the variable 'numbers' is not the data stored in the file, but the name of the file.<br>
> If your file name is pizza, then the variable 'numbers' holds the character string 'pizza' which you are then performing your analysis on. You need to address the variables stored in the data file, not the name of the file.<br>
> <br>
> <br>
> <br>
> <br>
> G^=EFe;M:WGGLWE99?WDGGWLHL@%=9F1QWMW^Wq9GA@w:=N;9WC9HA9D=@<br>
<br>
<br>
I understand what you're saying but Im not sure how to apply it. You're saying that in a way I'm just "renaming" the file. How do I fix it so that I just edit that data?

Mon, 19 Jan 2009 01:35:03 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622409
Matt Fig
I assume you are using Matfiles which store Matlab variables. If so then this should work.<br>
<br>
filename=input('What data file would you like to load? ','s');<br>
load(filename);<br>
<br>
clear filename<br>
who<br>
numbers = input('Which variable do you want to manupulate?')<br>
<br>
<br>
numbers(:).' % The file name.<br>
allnumbers=numbers(:).'<br>
length(allnumbers)<br>
l=length(allnumbers)<br>
disp(['Length of data ' num2str(l)])<br>
sum(allnumbers)<br>
s=sum(allnumbers)<br>
mean=s/l<br>
disp(['Your mean is ' num2str(mean)])<br>
<br>
<br>
<br>
<br>
<br>
7I]K?#?]]MWFAS?]?dJ@M+CkFG]]CNT?@JSFMR}d?EMMGKNLRAR?C]MCLw]

Mon, 19 Jan 2009 03:17:03 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622418
Jens
"Matt Fig" <spamanon@yahoo.com> wrote in message <gl0lc7$3ta$1@fred.mathworks.com>...<br>
> I assume you are using Matfiles which store Matlab variables. If so then this should work.<br>
> <br>
> filename=input('What data file would you like to load? ','s');<br>
> load(filename);<br>
> <br>
> clear filename<br>
> who<br>
> numbers = input('Which variable do you want to manupulate?')<br>
> <br>
> <br>
> numbers(:).' % The file name.<br>
> allnumbers=numbers(:).'<br>
> length(allnumbers)<br>
> l=length(allnumbers)<br>
> disp(['Length of data ' num2str(l)])<br>
> sum(allnumbers)<br>
> s=sum(allnumbers)<br>
> mean=s/l<br>
> disp(['Your mean is ' num2str(mean)])<br>
> <br>
<br>
Thank you for posting this. It works. Im just wondering, what do lines 46 of the code mean? The line that starts with clear filename and ends with numbers=input('Which variable do you want to manipulate?'). Our teacher never explained what the clear function does, and I think the who command displays what variables are present.

Mon, 19 Jan 2009 03:52:02 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622425
Matt Fig
"Jens " <jerald29@hotmail.com> wrote in message <br>
> Thank you for posting this. It works. Im just wondering, what do lines 46 of the code mean? The line that starts with clear filename and ends with numbers=input('Which variable do you want to manipulate?'). Our teacher never explained what the clear function does, and I think the who command displays what variables are present.<br>
<br>
For any Matlab function you can find out what it does by typing help function_name. So to find out what clear does, type:<br>
<br>
help clear<br>
<br>
I cleared the variable called filename before displaying the available variables so the user would not pick that variable to run the rest of the code on. Then, using who, I display the variable available to the user. If a clear command is issued before the filename is taken, the only variables that should be available are the ones stored in the file just loaded. <br>
An alternative approach can be had in the case when the variable stored in all of the files is the same. For example, say you have 10 files from which to choose, each has only one variable named X. Then there is no need for the second input request, just load the file of choice and do the operations on the variable X.<br>
<br>
<br>
<br>
<br>
sj,tp{ft,jyRhf%jftn?mJgt%qf%u%mf%gshztjrnyf%z%ytml~q^ufr3E%

Mon, 19 Jan 2009 15:40:40 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622527
Steven Lord
<br>
"Matt Fig" <spamanon@yahoo.com> wrote in message <br>
news:gl0lc7$3ta$1@fred.mathworks.com...<br>
>I assume you are using Matfiles which store Matlab variables. If so then <br>
>this should work.<br>
><br>
> filename=input('What data file would you like to load? ','s');<br>
> load(filename);<br>
><br>
> clear filename<br>
> who<br>
> numbers = input('Which variable do you want to manupulate?')<br>
><br>
><br>
> numbers(:).' % The file name.<br>
> allnumbers=numbers(:).'<br>
> length(allnumbers)<br>
> l=length(allnumbers)<br>
> disp(['Length of data ' num2str(l)])<br>
> sum(allnumbers)<br>
> s=sum(allnumbers)<br>
> mean=s/l<br>
> disp(['Your mean is ' num2str(mean)])<br>
<br>
Rather than doing this, I'd recommend LOADing the variables into a struct <br>
array, then using dynamic field name referencing to access the selected <br>
field of that struct array. "Borrowing" some of Matt's code:<br>
<br>
<br>
filename=input('What data file would you like to load? ','s');<br>
allTheData = load(filename);<br>
<br>
% Each variable in the file that was loaded is now stored in a field of the <br>
struct array allTheData<br>
% therefore, the list of fields in allTheData is the same as the list of <br>
variables from the file that was loaded<br>
% use FIELDNAMES to extract the list of fields.<br>
listOfVariables = fieldnames(allTheData);<br>
<br>
disp('The variables that were loaded are:')<br>
disp(listOfVariables)<br>
<br>
variableName = input('Which variable do you want to manipulate?', 's');<br>
<br>
% Now use dynamic field names to extract that field from the struct <br>
allTheData into the variable numbers<br>
% If you really wanted, you could validate that variableName is actually a <br>
member of listOfVariables<br>
% by using ISMEMBER and looping back (via a WHILE loop) to ask for a <br>
variable name if you wanted<br>
% to perform some basic error checking. I'm leaving this as an exercise to <br>
the reader ;)<br>
numbers = allTheData.(variableName);<br>
<br>
% Now you can do whatever you want with the numbers variable<br>
<br>
numbers(:).'<br>
allnumbers=numbers(:).'<br>
length(allnumbers)<br>
l=length(allnumbers)<br>
disp(['Length of data ' num2str(l)])<br>
sum(allnumbers)<br>
s=sum(allnumbers)<br>
themean=s/l % so as not to 'shadow' the builtin MEAN function<br>
disp(['Your mean is ' num2str(themean)])<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com

Mon, 19 Jan 2009 16:55:19 +0000
Re: Converting Matrix Dimensions?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/242607#622548
Matt Fig
Good point Steve. While the code is a bit more 'clunky' it does avoid confusion over loaded variables and those that were in the workspace prior to running this function.<br>
<br>
Thanks.<br>
<br>
<br>
<br>
<br>
pVX_R]STe6`VpY\p^RRT`Z~pRSefpYJ_RVap]+R`aZewjV1^ppw`Y`R>`fg