How to create a row cell from a meshgrid in MATLAB?

I have a library function that takes parameters as a text string (this is a common C library with MATLAB interface). I want to call it a set of parameters like this:

'-a 0 -b 1'
'-a 0 -b 2'
'-a 0 -b 3'
'-a 1 -b 1'
'-a 1 -b 2'
'-a 1 -b 3'

etc...

I am creating values aand bwith meshgrid:

[a,b] = meshgrid(0:5, 1:3);

which gives:

a =

 0     1     2     3     4     5
 0     1     2     3     4     5
 0     1     2     3     4     5

b =

 1     1     1     1     1     1
 2     2     2     2     2     2
 3     3     3     3     3     3

And now I want to somehow put them in a row cell:

params = {'-a 0 -b 1'; -a 0 -b 2 '; etc...}

I tried to use sprintf, but it only concatenates them

sprintf('-a %f -b %f', a ,b)

ans =

-a 0.000000 -b 0.000000-a 0.000000 -b 1.000000-a 1.000000 -b 1.000000-a 2.000000 -b 2.000000-a 2.000000 -b 3.000000-a 3.000000 -b 3.000000-a 4.000000 -b 4.000000-a 4.000000 -b 5.000000-a 5.000000 -b 5.000000-a 1.000000 -b 2.000000-a 3.000000 -b 1.000000-a 2.000000 -b 3.000000-a 1.000000 -b 2.000000-a 3.000000 -b 1.000000-a 2.000000 -b 3.000000-a 1.000000 -b 2.000000-a 3.000000 -b 1.000000-a 2.000000 -b 3.000000

With the exception of loops over aand b, how can I create the desired cell?

+3
source share
2 answers

, INT2STR STRCAT:

params = strcat({'-a '},int2str(a(:)),{' -b '},int2str(b(:)));
+3

:

strcat(num2str([a(:) b(:)],'-a %d -b %d'), {})
+2

Source: https://habr.com/ru/post/1735120/


All Articles