combine matrix and vector

조회 수: 3 (최근 30일)
Christian Jensen
Christian Jensen 2018년 5월 9일
편집: Stephen23 2018년 5월 9일
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
Stephen23
Stephen23 2018년 5월 9일
편집: Stephen23 2018년 5월 9일
"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'}'

댓글을 달려면 로그인하십시오.

답변 (1개)

Bob Thompson
Bob Thompson 2018년 5월 9일
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,:);

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by