MATLAB Answers

How to create table with existing arrays?

86 views (last 30 days)
Sumeet Badgujar
Sumeet Badgujar on 30 Jul 2018
Edited: Adam Danz on 30 Jul 2018
N=[1,3,4,7,9,12,13,16,19,21];
i=(1:10);
Q= sqrt(N(i)*3);
CI=1./(6*(Q.^-4));
CIinDB=10*log10(CI);
t=[N',Q',CI',CIinDB']
This is the code that I have written.I want the existing arrays to be made into a table where I can add column name.And the values should be converted in integers.

Answers (2)

Adam Danz
Adam Danz on 30 Jul 2018
Edited: Adam Danz on 30 Jul 2018
Here's how to put your variables into a table with column names.
table(N',Q',CI',CIinDB', 'VariableNames', {'N' 'Q' 'CI' 'CIinDB'})
"Convert them to integers" - use round() for each variable above or use your 't' variable as shown below.
array2table(round(t), 'VariableNames', {'N' 'Q' 'CI' 'CIinDB'})

Guillaume
Guillaume on 30 Jul 2018
I don't understand the difficulty you're facing. What's wrong with just using table to create the table directly from your variables, or array2table if you really want to go through the matrix?
N = N'; Q = Q'; Cl = Cl'; ClinDB = ClinDB'; %of course if you created N as a column vector you would not need to transpose anything
yourtable = table(N, Q, Cl, ClinDB);
or
yourtable = array2table(t, 'VariableNames', {'N', 'Q', 'Cl', 'ClinDB'});
It's all very well documented.
Note that, in
%N = a_vector_with_10_elements;
i = (1:10) %parentheses completely redundant. It's the same as i = 1:10
Q = sqrt(N(i)*3)
the little trip through i is a complete waste of time.
Q = sqrt(N)*3)
does exactly the same.

Community Treasure Hunt

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

Start Hunting!