how to make table

5 visualizzazioni (ultimi 30 giorni)
imola
imola il 8 Ago 2014
Modificato: imola il 11 Ott 2014
Dear all,
I need to make table of four rows such that I can sort it and turn it to a table contain the strings of the rows because I want to rank it, and apply GA on it and I used fprintf before but it is not like table,,,fprintf(' %f %f \n',f,l),,, I should use table, but I have problem how do it could you please help me, thanks in advance
for j=1:4
q1=[25.8862 3.0912; 4.9390 44.5138; 25.3633 5.6858; 4.5108 48.9112];
q2=[11.5684 5.9243; 1.6695 41.9517; 24.5223 29.9721; 32.6509 23.0566];
r=sqrt((q1(j,1)-q1(j,2))^2+(q2(j,1)-q2(j,2))^2);
f=h/2;l=f/3;
format LONG
A=[f,l];s = regexprep(sprintf('%011.6f',A),'[.\s]','');end
>>
  2 Commenti
Geoff Hayes
Geoff Hayes il 8 Ago 2014
imola - please format the above code so that it is readable. Highlight the code portion and press {} Code.
By table do you mean matrix? What would the four rows be since you have to input parents p1 and p2? How would you sort it i.e. what is the sorting criteria?
Image Analyst
Image Analyst il 8 Ago 2014
table is a new data type as or R2013b where you can mix data types like strings and numbers by column. A little more restricted than cell arrays but a mot easier to deal with if all your columns are the same data type for every row in that column.
In my answer I used a cell array, and a matrix (probably the easiest form).

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 8 Ago 2014
Try this:
clc; % Clear the command window.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
p1=[25.8862 3.0912; 4.9390 44.5138; 25.3633 5.6858; 4.5108 48.9112];
p2=[11.5684 5.9243; 1.6695 41.9517; 24.5223 29.9721; 32.6509 23.0566];
for k = 1:4
d=sqrt((p1(k,1)-p1(k,2))^2 + (p2(k,1)-p2(k,2))^2);
a=d/2;
b=a/3;
s{k} = sprintf('%011.6f, %011.6f', a, b);
end
celldisp(s)
In the command window:
s{1} =
0011.741677, 0003.913892
s{2} =
0028.234821, 0009.411607
s{3} =
0010.209118, 0003.403039
s{4} =
0022.712585, 0007.570862
Since your strings are all the same length, you can make s a character array instead of a cell array if you want.
s = char(4, 24);
for k = 1:4
d=sqrt((p1(k,1)-p1(k,2))^2 + (p2(k,1)-p2(k,2))^2);
a=d/2;
b=a/3;
s(k,1:24) = sprintf('%011.6f, %011.6f', a, b);
end
s
s =
0011.741677, 0003.913892
0028.234821, 0009.411607
0010.209118, 0003.403039
0022.712585, 0007.570862
  1 Commento
Image Analyst
Image Analyst il 9 Ago 2014
Geoff didn't give any code. Please make it easy for me. Please give code to construct your table.

Accedi per commentare.

Categorie

Scopri di più su Cell Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by