How to Combine Several Variables Into One Matrix?

21 ビュー (過去 30 日間)
Hannah Schultejans
Hannah Schultejans 2015 年 8 月 6 日
コメント済み: Gabriel Aviles 2020 年 5 月 16 日
Hello,
I have several variables, all saved as individuals, that I am processing. Each variable has one column of data in varying lengths. These are loaded into Matlab with a for loop, so as to automate the processing. I want to combine all of this data into a one-column matrix. I also eventually want to calculate standard deviation and the mean itself from the created matrix. How would I accomplish this?
Here's an example:
A =
3
4
5
B =
5
6
7
8
C =
3
2
I want to combine A, B, and C into a matrix, D, that would look like this:
D =
3
4
5
5
6
7
8
3
2
and then eventually average them so
E = mean(D)
and
F = std(D)
Please keep in mind that these variables are all saved separately. I don't know if that makes a difference (I'm kind of a newb) but if it does, please keep that in mind.
Thanks!!!

採用された回答

Star Strider
Star Strider 2015 年 8 月 6 日
In your for loop, the easiest way might be something like this:
D = [];
for k1 = 1: ...
v{k1} = load( ... );
D = [D; v{k1}];
end
This creates ‘D’ as part of the variable loading loop, so you have it when you have loaded all your variables. Your variables also exist without modification in the ‘v’ cell array.
  2 件のコメント
Hannah Schultejans
Hannah Schultejans 2015 年 8 月 6 日
This works! Now I have something a little more challenging- if the variables are structures, how do I achieve the same if I need to go into the variable to find the data (eg: variable A contains B and C, I want the data from C).
Star Strider
Star Strider 2015 年 8 月 6 日
I haven’t worked with structures in a while. I would isolate the variables as vectors, do the concatenation, and then put them back into the structure. (It very much depends on how the structure is defined.)

サインインしてコメントする。

その他の回答 (1 件)

James Tursa
James Tursa 2015 年 8 月 6 日
編集済み: James Tursa 2015 年 8 月 6 日
The easiest way would be to read your variables into elements of a column vector cell array instead of reading them in as individual named variables. Then the cell2mat function will do what you want.
EDIT:
I was assuming you had variable number of mat files you were dealing with which could change. But maybe your problem is simpler. Do you always have variables A, B, and C? Then you can just concatenate them:
D = [A;B;C];
If you indeed have a variable number of mat files you are dealing with, can you post how you are reading them in and how you know what the variable names are?
  2 件のコメント
Hannah Schultejans
Hannah Schultejans 2015 年 8 月 6 日
I'm not sure I'm understanding you correctly. I can't alter how they're read in. The data within the variables needs to remain separated.
Gabriel Aviles
Gabriel Aviles 2020 年 5 月 16 日
How can I concatenate variables with inconsistent dimensions ?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by