Hi,
I am new for matlab , can any one let me know how i can form vector of Integer with Comma .
Example :
for int i =1:2
matrix=[coordinate_x;ordinate_y;ordinate_z;ordinate_xx;ordinate_xy;ordinate_yy;ordinate_zx;ordinate_zy;];
%1-by-8 matrix
disp(matrix(:));
end
Result is of Matix is
29
45
140
45
82
12
82
80
39
65
40
75
92
02
32
80
Note 1 : I would like to form like matrix =[29,45,140,45,82,12,82,80] [39,65,40,75,92,02,32,80]

 Akzeptierte Antwort

Image Analyst
Image Analyst am 1 Mär. 2014

0 Stimmen

Commas add columns within the same row. Semicolons add another row:
matrix =[29,45,140,45,82,12,82,8; 39,65,40,75,92,02,32,80]
matrix will be a two row matrix. The second row starts after the semicolon.

6 Kommentare

SAMEER ahamed
SAMEER ahamed am 1 Mär. 2014
Bearbeitet: SAMEER ahamed am 1 Mär. 2014
Yes You are right ...when i add comma i have get result below like ?
if true
% code
matrix
Columns 1 through 7
28 40 158 40 79 1 79
Column 8
68
end
if true
% code
matrix
Columns 1 through 7
1 46 126 46 82 1 82
Column 8
76
end
Note:please let me know how i can form in array with comma each column? matrix =[28,40,158,40,79,1,79,68]
I thought I did. You're not giving exact syntax of either a statement of code, or how it spits things out to the command window so perhaps I guessed wrong at what you're looking to get. This is what I get:
>> matrix =[29,45,140,45,82,12,82,8; 39,65,40,75,92,02,32,80]
matrix =
29 45 140 45 82 12 82 8
39 65 40 75 92 2 32 80
The first is the line of code and because I didn't put a semicolon at the end of the line to suppress output, it sends the output to the command window and that is what shows up. Now, exactly what did you want, if it's not that?
"form in array with comma each column" does not make grammatical sense. Perhaps you can get a native English speaker to look it over. Maybe you want a string or character array - that's the only way you can have commas because a numerical matrix is just a collection of numbers:
outputString = sprintf('%d, ', matrix);
SAMEER ahamed
SAMEER ahamed am 1 Mär. 2014
Bearbeitet: SAMEER ahamed am 1 Mär. 2014
Thank's for reply me , now i have got each column with comma , now i want to add [] . example :
if true
% code
outputString = sprintf('%d, ', matrix);
>> disp(outputString)
29, 45, 140, 45, 82, 12, 82, 8, %How to remove last column 8 comma ? also in between arrange [] array.
end
outputString1 = sprintf('%d, ', matrix(1,:));
outputString2 = sprintf('%d, ', matrix(2,:));
outputString = sprintf('[%s] [%s]', outputString1, outputString2);
Seems like a weird thing to want to do, but whatever...
Thank you so much reply to me ...Now I got below result like .
Example :
disp(outputString);
outputString =[1, 2, 3, 4, 5, 6, 7, 8, ][1, 2, 3, 4, 5, 6, 7, 8, ];
*Note-1 : May i know how i can remove comma after 8 .
Note-2 : I would like to get like outputString =[1, 2, 3, 4, 5, 6, 7, 8][1, 2, 3, 4, 5, 6, 7, 8];*

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Kun
David Kun am 27 Jan. 2017

0 Stimmen

I ran into the same problem. Here's a code snippet on how I solved it.
matrix =[29,45,140,45,82,12,82,8; 39,65,40,75,92,02,32,80]
[r,c] = size(matrix);
sprintf([ '[' [repmat('%d,', 1, c-1) repmat('%d', 1, 1)] ']'], matrix')
The output is
[29,45,140,45,82,12,82,8][39,65,40,75,92,2,32,80]
Note, there's no semicolon after the sprintf statement; this emits the output to the console. The first argument in the sprintf() is creating a format string to print the values of each row of the matrix. If c=2, then the format string '[%d, %d]' is created. If you need carriage returns in between each row, you can do the following.
sprintf([ '[' [repmat('%d,', 1, c-1) repmat('%d', 1, 1)] ']\n'], matrix')

Kategorien

Mehr zu Just for fun finden Sie in Hilfe-Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by