Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Converting Matrix Dimensions?

Subject: Converting Matrix Dimensions?

From: Jens Doe

Date: 17 Jan, 2009 21:54:02

Message: 1 of 12

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

Subject: Converting Matrix Dimensions?

From: Sadik

Date: 18 Jan, 2009 01:00:04

Message: 2 of 12

"Jens Doe" <jerald29@hotmail.com> wrote in message <gktk1p$7ak$1@fred.mathworks.com>...
> 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

Hello Jens,

Let us call your data matrix as dataMatrix. Then, the following will give the 1x80 version of your data:

dataMatrix(:).'

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.].

Subject: Converting Matrix Dimensions?

From: Jos

Date: 18 Jan, 2009 15:57:01

Message: 3 of 12

"Jens Doe" <jerald29@hotmail.com> wrote in message <gktk1p$7ak$1@fred.mathworks.com>...
> 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

In general, the function RESHAPE is designed to, well, reshape an array. If you want to end up with a vector (N-by-1 or 1-by-N shapes) use reshape with an empty argument, or you can use the colon operation (followed by a transpose)
 
Example: reshape(A,[],1) will reshape an array A into a column vector

hth
Jos

Subject: Converting Matrix Dimensions?

From: Jens Doe

Date: 19 Jan, 2009 00:26:01

Message: 4 of 12

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

using (:).'

numbers=input('What data file would you like to load? ','s')
    
    load(numbers)
    
    numbers(:).'
    
    allnumbers=numbers(:).'
    
    length(allnumbers)
    
    l=length(allnumbers)
    
    disp(['Length of data ' num2str(l)])
    
   sum(allnumbers)
   
   s=sum(allnumbers)
   
   mean=s/l
   
   disp(['Your mean is ' num2str(mean)])

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.


Then using the reshape(A,[],1)

clear
clc

data=input('What file would you like to load? ','s')

load(data)

a=reshape(data,[],1)

disp(['Length of data ' num2str(a)])


With this I'm getting an error of
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.

I'm not too sure whats wrong with either.

Subject: Converting Matrix Dimensions?

From: Sadik

Date: 19 Jan, 2009 00:43:01

Message: 5 of 12

"Jens Doe" <jerald29@hotmail.com> wrote in message <gl0hap$83m$1@fred.mathworks.com>...
> 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
>
> using (:).'
>
> numbers=input('What data file would you like to load? ','s')
>
> load(numbers)
>
> numbers(:).'
>
> allnumbers=numbers(:).'
>
> length(allnumbers)
>
> l=length(allnumbers)
>
> disp(['Length of data ' num2str(l)])
>
> sum(allnumbers)
>
> s=sum(allnumbers)
>
> mean=s/l
>
> disp(['Your mean is ' num2str(mean)])
>
> 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.
>
>
> Then using the reshape(A,[],1)
>
> clear
> clc
>
> data=input('What file would you like to load? ','s')
>
> load(data)
>
> a=reshape(data,[],1)
>
> disp(['Length of data ' num2str(a)])
>
>
> With this I'm getting an error of
> ??? Error using ==> horzcat
> CAT arguments dimensions are not consistent.
>
> I'm not too sure whats wrong with either.

Hello Jens,

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.

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 10-element vector with a row string. This should help:

disp(['Length of data ' num2str(length(a))])

Hope this helps.

Subject: Converting Matrix Dimensions?

From: Matt Fig

Date: 19 Jan, 2009 00:47:01

Message: 6 of 12

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.
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.




G^=EFe;M:WGGLWE99?WDGGWLHL@%=9F1QWMW|^Wq9GA@w:=N;9WC9HA9D=@

Subject: Converting Matrix Dimensions?

From: Jens

Date: 19 Jan, 2009 01:22:02

Message: 7 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <gl0ii5$ru9$1@fred.mathworks.com>...
> 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.
> 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.
>
>
>
>
> G^=EFe;M:WGGLWE99?WDGGWLHL@%=9F1QWMW|^Wq9GA@w:=N;9WC9HA9D=@


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?

Subject: Converting Matrix Dimensions?

From: Matt Fig

Date: 19 Jan, 2009 01:35:03

Message: 8 of 12

I assume you are using Matfiles which store Matlab variables. If so then this should work.

filename=input('What data file would you like to load? ','s');
load(filename);

clear filename
who
numbers = input('Which variable do you want to manupulate?')


numbers(:).' % The file name.
allnumbers=numbers(:).'
length(allnumbers)
l=length(allnumbers)
disp(['Length of data ' num2str(l)])
sum(allnumbers)
s=sum(allnumbers)
mean=s/l
disp(['Your mean is ' num2str(mean)])





7I]K?#?]]MWFAS?]?dJ@M+CkFG]]CNT?@JSFMR}d?EMMGKNLRAR?C]MCLw]

Subject: Converting Matrix Dimensions?

From: Jens

Date: 19 Jan, 2009 03:17:03

Message: 9 of 12

"Matt Fig" <spamanon@yahoo.com> wrote in message <gl0lc7$3ta$1@fred.mathworks.com>...
> I assume you are using Matfiles which store Matlab variables. If so then this should work.
>
> filename=input('What data file would you like to load? ','s');
> load(filename);
>
> clear filename
> who
> numbers = input('Which variable do you want to manupulate?')
>
>
> numbers(:).' % The file name.
> allnumbers=numbers(:).'
> length(allnumbers)
> l=length(allnumbers)
> disp(['Length of data ' num2str(l)])
> sum(allnumbers)
> s=sum(allnumbers)
> mean=s/l
> disp(['Your mean is ' num2str(mean)])
>

Thank you for posting this. It works. Im just wondering, what do lines 4-6 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.

Subject: Converting Matrix Dimensions?

From: Matt Fig

Date: 19 Jan, 2009 03:52:02

Message: 10 of 12

"Jens " <jerald29@hotmail.com> wrote in message
> Thank you for posting this. It works. Im just wondering, what do lines 4-6 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.

For any Matlab function you can find out what it does by typing help function_name. So to find out what clear does, type:

help clear

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.
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.




sj,tp{ft,jyRhf%jftn?mJgt%qf%u%mf%gshztjrnyf%z%ytml~q^ufr3E%

Subject: Converting Matrix Dimensions?

From: Steven Lord

Date: 19 Jan, 2009 15:40:40

Message: 11 of 12


"Matt Fig" <spamanon@yahoo.com> wrote in message
news:gl0lc7$3ta$1@fred.mathworks.com...
>I assume you are using Matfiles which store Matlab variables. If so then
>this should work.
>
> filename=input('What data file would you like to load? ','s');
> load(filename);
>
> clear filename
> who
> numbers = input('Which variable do you want to manupulate?')
>
>
> numbers(:).' % The file name.
> allnumbers=numbers(:).'
> length(allnumbers)
> l=length(allnumbers)
> disp(['Length of data ' num2str(l)])
> sum(allnumbers)
> s=sum(allnumbers)
> mean=s/l
> disp(['Your mean is ' num2str(mean)])

Rather than doing this, I'd recommend LOADing the variables into a struct
array, then using dynamic field name referencing to access the selected
field of that struct array. "Borrowing" some of Matt's code:


filename=input('What data file would you like to load? ','s');
allTheData = load(filename);

% Each variable in the file that was loaded is now stored in a field of the
struct array allTheData
% therefore, the list of fields in allTheData is the same as the list of
variables from the file that was loaded
% use FIELDNAMES to extract the list of fields.
listOfVariables = fieldnames(allTheData);

disp('The variables that were loaded are:')
disp(listOfVariables)

variableName = input('Which variable do you want to manipulate?', 's');

% Now use dynamic field names to extract that field from the struct
allTheData into the variable numbers
% If you really wanted, you could validate that variableName is actually a
member of listOfVariables
% by using ISMEMBER and looping back (via a WHILE loop) to ask for a
variable name if you wanted
% to perform some basic error checking. I'm leaving this as an exercise to
the reader ;)
numbers = allTheData.(variableName);

% Now you can do whatever you want with the numbers variable

numbers(:).'
allnumbers=numbers(:).'
length(allnumbers)
l=length(allnumbers)
disp(['Length of data ' num2str(l)])
sum(allnumbers)
s=sum(allnumbers)
themean=s/l % so as not to 'shadow' the built-in MEAN function
disp(['Your mean is ' num2str(themean)])

--
Steve Lord
slord@mathworks.com

Subject: Converting Matrix Dimensions?

From: Matt Fig

Date: 19 Jan, 2009 16:55:19

Message: 12 of 12

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.

Thanks.




pVX_R]STe6`VpY\p^RRT`Z~pRSefpYJ_RVap]+R`aZewjV1^ppw`Y`R>`fg

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us