How to create a table and add elements to the table in MATLAB?
56 ビュー (過去 30 日間)
古いコメントを表示
surabhi sachdeva
2017 年 10 月 12 日
コメント済み: surabhi sachdeva
2017 年 10 月 18 日
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 件のコメント
採用された回答
Walter Roberson
2017 年 10 月 13 日
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 件のコメント
Walter Roberson
2017 年 10 月 17 日
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,:,:)
その他の回答 (1 件)
Guillaume
2017 年 10 月 13 日
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'};
1 件のコメント
Walter Roberson
2017 年 10 月 13 日
The user is using R2013a, which is the release before table objects were added.
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!