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

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 Comments

Finally, I made the table and now having difficulty in adding elements to the table. Please help
Sorry, can't understand the issue you're having. You can't add a variable to a table without having values for all rows, though; it has to be a rectangular structure.
Show us a small subset of the problem -- what you have and what you'd like the result to be (and logic to get the latter from the former).
Sir, I have set of inputs with me. I want to add elements to the empty cells like the one I am attaching.
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.

Sign in to comment.

 Accepted Answer

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 Comments

Won't it be possible to add 'lambda' in only some selected cells and not to all the cells of 6x6?
I want to learn this. I am totally confused.
Please help.
states = {'0', '1', '2', '3', '5', '6'};
rownames = states;
prefix = 't';
varnames = strcat({prefix}, states);
initial = cell(6, 6);
initial(1:3, [2 5]) = {'lambda'}; %put in a range
initial(4, 6) = {'lambda'}; %put in individuals
MyTable = mat2dataset(initial, 'VarNames', varnames, 'ObsNames', rownames);
Thank you so much for your help
Regards Surabhi
states = ...
{'000010', '100010', '200010','110010', '210010', '001010', '101010', '201010', '011010', '111010', '211010', '002010', '102010', '202010', '012010', '112010', '212010', '000110', '100110', '200110', '010110', '110110', '210110','001110', '101110', '201110', '011110', '111110', '211110', '002110', '102110', '202110', '012110', '112110', '212110' };
rownames = states; prefix = 'T';
varnames = strcat({prefix}, states);
selected = cell(35, 35);
SelectedTable = mat2dataset(selected, 'VarNames', varnames, 'ObsNames', rownames);
By using the above code, I got a 35X35 dataset. I want to apply transition rules on the dataset I got. I want the MATLAB program to automatically check for the states, which state verifying which rule? On the basis of which, MATLAB should automatically allocate values to this dataset.
Suppose states are represented as [i,j,k,l,m,n].
According to the rule,
for i>0
if {i,j,k,l,m,n}=={i+1, j, k, l, m ,n)
then, it should display " blue"
else if
for j=0,1 && i=1
{i,j,k,lm,n}=={i, j+1, k, l, m, n}
then it should display "green"***
The MATLAB program must automatically check the states and according to rules, it must specify the message. Is this automatic checking possible?
Please help
Regards
Surabhi
No, you will need to write a program to set the results that you want.
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.
I assume here that "display" means to set the corresponding entry in SelectedTable.
%when setting entries in the cell, take into account that there is
%an extra row for the variable names and an extra column for
%observation names
result_cell = dataset2cell(SelectedTable);
consider_this_state_mask = cellfun( @(s) s(1) ~= '0', states);
[targ_exists,targnum] = cellfun(@(s) ismember(char(s+[1,0,0,0,0,0]), states), states)
selsrc = find(consider_this_state_mask & targ_exists);
seltarg = targnum(selsrc);
blue_matches_mask = StateValue(selsrc) == StateValue(seltarg);
blue_match_idx = sub2ind(size(result_cell), 1+selsrc(blue_matches_mask), 1+seltarg(blue_matches_mask));
result_cell(blue_match_idx) = {'blue'};
consider_this_state_mask = cellfun( @(s) s(1) ~= '1' & ismember(s(2), {'0', '1'}), states);
[targ_exists,targnum] = cellfun(@(s) ismember(char(s+[0,1,0,0,0,0]), states), states)
selsrc = find(consider_this_state_mask & targ_exists);
seltarg = targnum(selsrc);
green_matches_mask = StateValue(selsrc) == StateValue(seltarg);
green_match_idx = sub2ind(size(result_cell), 1+selsrc(green_matches_mask), 1+seltarg(green_matches_mask));
green_match_idx = setdiff(green_match_idx, blue_match_idx);
result_cell(green_match_idx) = {'green'};
NewSelectedTable = cell2dataset(result_cell, 'ReadVarNames', true, 'ReadObsNames', true);
I am looking for this sort of matrix. How can it be made using MATLAB for all the 729 input elements? For the 729X729 matrix.
Can you please help regarding this?
As we discussed a number of Questions back, the only way to produce that display is to text() all of it into place on a display that is at least 32000 pixels wide.
table objects and dataset objects cannot display text with subscripts. With your R2013a version, if you are using MS Windows, then I gather that you cannot even put the Greek Characters into a dataset object and have them display properly, but that might also depend upon your exact MS Windows version and which fonts you have installed. Either way, table objects and dataset objects will always display '' around character strings, which is another reason you would have to text() everything into a graphics display.
Sir, The authors wrote that by using MATLAB, they have found steady-state probabilities and made this table. Could you please help me find that?
If the authors wrote that using MATLAB, then they must have text()'d everything into place for use on a display (or set of displays) more than 30000 pixels wide in total.
I would offer you a hypothesis that I think has a much much higher probability of being true:
That the authors used a numeric array rather than a dataset object or table object, and that they examined it in numeric form, and that when they wrote up their results for the paper, they created a little diagram to show the pattern of entries.
If the authors had asked my opinion, I would have told them 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.
Sir, they have shown only six states in the dataset and They have given state transition rules for finding the data set of the steady state probability.
And, I have found out there are more input states possible but I am like messed up on how to get the complete table of that type?
Can you please help me regarding this?
How to complete the table?
How can I generate the steady-state probabilities of the complete table?
All I have seen is a diagram, not a table. I repeat:
  • if you need it to actually say things like mu subscript p then you need to text() everything into place onto an image that is more than 30000 pixels wide.
  • If you need text that does not have subscripts or (taking into account your release) greek characters, then you could use character vectors in every entry of the dataset, but those will display with '' around each character vector.
  • If you need text that does not have subscripts or (taking into account your release) greek characters, and it is not acceptable to have '' show up around it, then you will need to construct a character array, probably around 2500 characters wide, and then you would write that to a file or disp() it at the command line.
  • If what you need to do is compute with it, then Yes, you can do that with a dataset, but my recommendation would be to use a 12 dimensional array, which makes several of the transition rules very easy to express.
"The authors wrote that by using MATLAB, ..."
As Walter says, the authors may have computed results with Matlab, they certainly did NOT produce the printed document with Matlab.
You're barking up the wrong tree with the wrong tool for the purpose; as a (very smart fella', is Walter) says, compute the result but write up the answers with something other than Matlab that is built for publishing.
There are rules attached. Also, the state transition rate matrix attached. I want to complete this state transition rate matrix, Won't it be possible through MATLAB?
Is it a literal lambda subscript p that has to go into those entries? Or is it a numeric probability whose values follow the pattern given by those labels?
When you look at the data structures while you are debugging the calculation do you need to see a nice 2D table with text entries and no quote marks being stored as the data structure?
Or do you just need to be able to check that the calculation is working?
Or would it be acceptable to calculate in a less formatted data structure and then format as text to be printed out on any handy nearby printer that has a 21 1/2 metre wide page (paper size 10A0) ? (Hmmm, you might need paper size 11A0 to get the full length of the table, I would have to work that out.) (Ummmm, it sure looks like I might be off by a factor of 10 in my calculation, maybe you only need paper size 7A0 or 8A0?)
Sir, It is a numeric probability whose values follow the pattern given by those labels.
Yes sir, Table should be like all the inputs with the entries as per transition rules must be visible(2D table or yes it would be acceptable with a less formatted data structure as well)
further, it's values are required to carry out the calculation.
This table is to be completed so as to continue working further.
Kindly help
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,:,:)
Sir,
Thank you
I am trying the same.

Sign in to comment.

More Answers (1)

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 Comment

The user is using R2013a, which is the release before table objects were added.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!