How to align names with the numbers in a matrix?

조회 수: 5 (최근 30일)
Karanvir singh Sohal
Karanvir singh Sohal 2021년 4월 5일
편집: Adam Danz 2021년 4월 6일
Hello!
I have to put heading row over the data matrix so I used the following code:
a=[12 13 14];
b={'a'; 'b'; 'c';};
c=[char(b)'; num2str(a)]
it gives me:
C= abc
12 13 14
But I want to align the first row over the second one.
required output:
C= a b c
12 13 14
  댓글 수: 2
David Fletcher
David Fletcher 2021년 4월 5일
Can't you use a table - using the entries in b as the VariableNames and a as the row data
Karanvir singh Sohal
Karanvir singh Sohal 2021년 4월 5일
편집: Karanvir singh Sohal 2021년 4월 5일
This is what I am looking for.
I am confused about how to use it for my case @David Fletcher.
Name={'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' };
Data=[ 1 2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31 32 33];
Name is 1x11 array
Data is 3x11
I can assign variables as
a=Data(:,1)....
but thats a lengthy process and my matrix size can vary.
How can I use table function so that I have "Name" as heading row of table and "Data" matrix as its value.
Required output:
a b c d ......
__________ ______ ______ ______ ______
1 2 3 4 ......
12 13 14 15 ......
23 24 25 26 ......

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

채택된 답변

Karanvir singh Sohal
Karanvir singh Sohal 2021년 4월 6일
So, finally I found what I was looking for
Name={'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' };
Data=[ 1 2 3 4 5 6 7 8 9 10 11; 12 13 14 15 16 17 18 19 20 21 22; 23 24 25 26 27 28 29 30 31 32 33];
T=struct2table(cell2struct(num2cell(Data,1),Name,2));
Thanks :)
  댓글 수: 1
Adam Danz
Adam Danz 2021년 4월 6일
편집: Adam Danz 2021년 4월 6일
much simpler:
T = array2table(Data,'VariableNames',Name)

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 4월 5일
a=[12 13 14];
b={'a'; 'b'; 'c';};
fprintf('%5s %5s %5s\n', b{:}); fprintf('%5d %5d %5d\n', a);
a b c 12 13 14
  댓글 수: 1
Karanvir singh Sohal
Karanvir singh Sohal 2021년 4월 5일
@Walter Roberson If I have huge data this method messes up and made the output hard to read :(.
Is there another method by which "a" and "b" can be included in same array or matrix?

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by