How to create a table and add elements to the table in MATLAB?

49 visualizzazioni (ultimi 30 giorni)
I want to create a 10X10 table and add elements to the table. Rows and columns both are having entries 1 2 3 4 5 6 7 8 9 10] and also for binary entries as [ 001; 010; 110; 101; 210; 220; 002; 202; 222; 111]
e.g. I want to add elements under all the pairs of rows and columns like (1,1) (1,2).............................................(10,1)........... (10,10)
How can I add 'lambda' under these pairs of rows and columns using MATLAB?
Kindly tell
Regards
Surabhi
  4 Commenti
surabhi sachdeva
surabhi sachdeva il 13 Ott 2017
Sir, I have set of inputs with me. I want to add elements to the empty cells like the one I am attaching.
Guillaume
Guillaume il 13 Ott 2017
It is important to use the correct name for things. You are not creating tables, you are creating datasets (which are obsolete, due to be removed, and require the stats toolbox). The two are different.

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 13 Ott 2017
states = {'0', '1', '2', '3', '5', '6'};
rownames = states;
prefix = 't';
varnames = strcat({prefix}, states);
initial = repmat( {'lambda'}, 6, 6);
MyTable = mat2dataset(initial, 'VarNames', varnames, 'ObsNames', rownames);
  17 Commenti
Walter Roberson
Walter Roberson il 17 Ott 2017
In that case, my advice would be to compute using a 12 dimensional numeric array, and then to reshape() it for examination, possibly in the form of a uitable() as those allow scrolling.
As I posted before:
The easiest approach is to use a matrix
M = zeros(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);
which you would index as
M(Si, Sj, Sk, Sl, Sm, Sn, Di, Dj, Dk, Dl, Dm, Dn)
where the S* variables are "source" (where you are starting from) and the D* variables are "destination" (where you are going to)
Just remember that indices need to start from 1, so if you are working with 0, 1, 2 values then add 1 before using those as indices.
In the above, I posted,
"When you write {i,j,k,l,m,n}=={i+1, j, k, l, m ,n) I have to assume that you have a value of some sort associated with each state. Below, I call that StateValue."
That one-value-per-state array, StateValue as I named it above, would then be constructed with
StateValue = zeros(3, 3, 3, 3, 3, 3);
which you would index as
StateValue(Si, Sj, Sk, Sl, Sm, Sn)
Just remember that indices need to start from 1, so if you are working with 0, 1, 2 values then add 1 before using those as indices.
The rules
if {i,j,k,l,m,n}=={i+1, j, k, l, m ,n)
for j=0,1 && i=1
{i,j,k,lm,n}=={i, j+1, k, l, m, n}
would then be expressed as:
M_2D = reshape(M, 3^6, 3^6); %current M, reshaped to 2D
mask = StateValue(1:end-1,:,:,:,:,:) == StateValue(2:end,:,:,:,:,:);
idx = find(mask);
M_2D(idx, idx+1) = ... whatever numeric value
mask = StateValue(1+[1], 1+[0,1], :, :, :, :) == StateValue([1]+1, 1+[0,1]+1, :, :, :, :);
idx = find(mask);
M_2D(idx, idx+size(StateValue,1)) = .... whatever numeric value
...
M = reshape(M_2D, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3);
The 12-D shape is easier for expressing the rules, but the 2D shape is typically easier for the vectorized setting of the results.
The code M_2D(idx, idx+1) = and M_2D(idx, idx+size(StateValue,1)) = take advantage of the way that memory is laid out in arrays, where Array(I+1,:) is always the immediate next entry after Array(I,:), and Array(:,J+1) is size(Array,1) after Array(:,J). For example in a 3 x 2 array, the elements are laid out in the order
1 4
2 5
3 6
and Array(3,2) is 1 past Array(2,2) and size(Array,1)=3 past Array(3,1). Likewise in 6D, Array(:,:,K+1,:,:,:) is size(Array,1)*size(Array,2) past Array(:,:,K,:,:,:) and Array(:,:,:,L+1,:,:) is size(Array,1)*size(Array,2)*size(Array,3) past Array(:,:,:,L,:,:)

Accedi per commentare.

Più risposte (1)

Guillaume
Guillaume il 13 Ott 2017
If you do want to create tables as opposed to datasets, here is how I'd do it:
states = 0:8;
rownames = compose('%d', states);
varnames = compose('t_%d', states);
mytable = cell2table(cell(numel(states)), 'VariableNames', varnames, 'RowNames', rownames)
I'm not sure what your question is exactly, if you're looking to put a value in a particular cell of the table, you can use any number of syntaxes:
mytable(5, 6) = {'001'}; %using normal indices as for matrices
mytable('2', 't_4') = {'100'} %using row and var names
mytable.t_3(5) = {'101'};

Community Treasure Hunt

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

Start Hunting!

Translated by