combine matrix and vector
3 views (last 30 days)
Show older comments
Hi guys been struggling a bit with combining matrix and vectors to one big matrix. i have the following data:
headliner=[{'ID' 'Name' 'assignment1' 'assignment2' 'assignment3' 'finalgrade'}]
IDName=[{'s1' 's2' 's3' 's4'} ;{'aa' 'ab' 'ac' 'ad'}]'
grades=[-3 0 2 ;4 7 10; 12 10 2; 4 0 -3]
finalgrade=[-3 4 4 -3]'
is there a way i can combine all four so i can have one big matrix?
thank you in advance! Sincerely Christian
1 Comment
Stephen23
on 9 May 2018
Edited: Stephen23
on 9 May 2018
"is there a way i can combine all four so i can have one big matrix?"
Of course, there are lots of ways to "combine" matrices and vectors. But you did not tell us how you want a 1x6 cell, a 4x2 cell, 4x3 numeric, and a 4x1 numeric to be combined into one array. What should the output look like? What MATLAB version are you using?
Note that for the first two variables the concatenation operation is totally superfluous: in the first case you are not concatenating anything, and in the second case you can simply define a matrix using the cell braces:
headliner = {'ID','Name','assignment1','assignment2','assignment3','finalgrade'};
IDName = {'s1','s2','s3','s4';'aa','ab','ac','ad'}'
Answers (1)
Bob Thompson
on 9 May 2018
I would suggest building this into a structure array. It might take a bit more manual input, but you can set it up so that you have fields for each of your headliners and then corresponding data for each of the other bits of information.
for k = 1:4;
[data(k).ID] = IDName{1}(k);
[data(k).Name] = IDName{2}(k);
[data(k).assignment1] = grades(k,1);
[data(k).assignment2] = grades(k,2);
[data(k).assignment3] = grades(k,3);
[data(k).finalgrade] = finalgrade(k);
end
You will now have a structure array with six fields, ID, Name, assignment1-3, finalgrade, each with four entries for each of the four students.
For personal organization I would suggest replacing the individual assignment inputs with a single assignment array. Structure arrays are similar to cells in that they can contain any type of data within a field.
[data(k).assignments] = grades(k,:);
0 Comments
See Also
Categories
Find more on Operators and Elementary Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!