hsi
hsi

Reputation: 97

How can I read data from a table, and after that write and sort the data?

I have a table with a column of names and numbers.

I want to read the numbers of the table into a matrix, and then calculate some measure, write the result of that calculation as a new column at the end of the table, and after that sort the table according to the new column data.

My code is below:

`clc;
close all;
clear all;
Table1 = readtable('finalexcel.csv');
n = 7; 
pz = 100; 
a = rand(100,7);
zero = zeros(pz,1);
a = bsxfun(@rdivide,a.',sum(a.')).';
population = zeros(pz,n);
population = rand(100,7);
population = bsxfun(@rdivide,population.',sum(population.')).';%create     random number with sum equal 1 in each row
population = [population zero];

emp=146;
zero1 = zeros(emp,1);
CentralityMeasure  = Table1(:,4:11);
TPopulation = population';
 Measure = CentralityMeasure(:,1:7) * TPopulation(1:7,1);`

I get the following error:

Undefined function 'mtimes' for input arguments of type 'table'.

How can I read data from a table, and after that write and sort the data?

Thanks for your time.

Upvotes: 1

Views: 155

Answers (2)

Carlos Borau
Carlos Borau

Reputation: 1473

To insert a new column to any matrix (M) you can either use horzcat matlab function or just add it like:

M = [M, newcolumn]; % obviously the new column must have the same number of rows

To short it according to your newcolumn values do this:

[b,i] = sort(M(:,idx)); % idx is the index of your new column

sortedM = M(i,:);

You can also check further options for the sorting function in matlab

Upvotes: 0

OmG
OmG

Reputation: 18858

"mtimes" error is for matrix multiplication. As you can't do matrix operation over table, you should change the table into the matrix using table2array:

CentralityMeasure = table2array(Table1(:,4:11));

After this, you can using array2table to revert and write to the original table (Table1);

Upvotes: 0

Related Questions