How to use fprintf to show collated values for two arrays.
25 views (last 30 days)
Show older comments
I have filtered a large amount of data and found X and Y values that I need to print:
X = [189;189;189;190;190;190;191;191;191]
Y = [299;300;301;299;300;301;299;300;301]
To clarify, I need to print the first value of X with the first value of Y, then the second value of X with the second value of Y, and so on and so forth.
I currently have the following code:
fprintf("(%d,%d) \n", X, Y);
However, this is the output that I am given:
(189,189)
(189,190)
(190,190)
(191,191)
(191,299)
(300,301)
(299,300)
(301,299)
(300,301)
As you can see, all nine values of X are printed first, then all nine values of Y.
I know I could use the following, but I was hoping there is a more efficient sollution.
fprintf("(%d,%d) \n", X(1), Y(1));
fprintf("(%d,%d) \n", X(2), Y(2));
fprintf("(%d,%d) \n", X(3), Y(3));
fprintf("(%d,%d) \n", X(4), Y(4));
fprintf("(%d,%d) \n", X(5), Y(5));
fprintf("(%d,%d) \n", X(6), Y(6));
fprintf("(%d,%d) \n", X(7), Y(7));
fprintf("(%d,%d) \n", X(8), Y(8));
fprintf("(%d,%d) \n", X(9), Y(9));
2 Comments
Stephen23
on 21 Nov 2020
More efficient than using a loop:
X = [189;189;189;190;190;190;191;191;191];
Y = [299;300;301;299;300;301;299;300;301];
fprintf("(%d,%d) \n",[X,Y].')
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!