kojikurac
kojikurac

Reputation: 25

How to use answer (from command window) for a code

I have random matrix(A) and I find a result I'd like to use later for my code

A=randint(5,7,[1,9])
ans A =

     8     1     2     2     6     7     7
     9     3     9     4     1     7     1
     2     5     9     9     8     4     3
     9     9     5     8     9     6     1
     6     9     8     9     7     2     1

How can I now get:

A = [8,1,2,2,6,7,7;9,3,9...7,2,1];

without having to type it myself.

Upvotes: 1

Views: 73

Answers (3)

Amro
Amro

Reputation: 124563

MATLAB has a function for that: MAT2STR

>> A = randi([1,9],[5,7]);
>> mat2str(A)
ans =
[5 5 7 5 3 2 5;5 6 5 3 8 4 1;9 8 8 1 7 9 6;1 5 5 1 8 6 3;3 4 5 8 9 9 5]

This is suitable for use with EVAL

Upvotes: 1

bdecaf
bdecaf

Reputation: 4732

Just thought of another way. Your goal is to have A in your script - right?

You can just paste it as follows:

A = [
     8     1     2     2     6     7     7
     9     3     9     4     1     7     1
     2     5     9     9     8     4     3
     9     9     5     8     9     6     1
     6     9     8     9     7     2     1 
 ]

(note the square brackets)

It will evaluate to your original matrix.

Upvotes: 0

bdecaf
bdecaf

Reputation: 4732

Make the string yourself:

Str = ['[' sprintf('%i',A(1)) sprintf(',%i',A(2:end)) ']']

Note this string does not contain any ; as in your example. so when you evaluate it you will get a 1x35 vector (instead of the original 5x7matrix)

So easiest way to fix this would be to add after you evaluate the string.

A = reshape(A,5,7)

It will look like

B = [....
B = reshape(B,5,7)

Upvotes: 0

Related Questions