i want to get this matrix, can anyone help me ?
pi 0 0 1
0 pi 1 0
0 1 pi 0
1 0 0 pi

 Accepted Answer

eye(4) * pi + fliplr(eye(4))
would be one easy way.

3 Comments

mimalo salina
mimalo salina on 9 Oct 2017
Edited: mimalo salina on 9 Oct 2017
thank you Mr Guillaume for your help, but this way will give pi as 3.1416 , i want pi as a character number, if you can help me ?
That will give you pi as the closest representation to pi as a double which is 3.1415926535897931
If you want it do display the symbol pi then you'll need the symbolic toolbox which I know nothing about.
Note: the change the way matlab displays numbers use format. This only affects display, the numbers are stored in memory with maximum precision whichever format setting you use.
Pi = sym('pi');
diag([Pi Pi Pi Pi]) + fliplr(eye(4))
Or, R2016b or later,
Pi = char(hex2dec('03C0'));
t = string(fliplr(eye(4)));
t(1:5:end) = Pi;
t =
4×4 string array
"π" "0" "0" "1"
"0" "π" "1" "0"
"0" "1" "π" "0"
"1" "0" "0" "π"
or you could,
Pi = char(hex2dec('03C0'));
t = num2cell(fliplr(eye(4)));
t(1:5:end) = {Pi};
disp(t)
'π' [0] [0] [1]
[0] 'π' [1] [0]
[0] [1] 'π' [0]
[1] [0] [0] 'π'

Sign in to comment.

More Answers (1)

Here's an even simpler way:
m = [...
pi 0 0 1
0 pi 1 0
0 1 pi 0
1 0 0 pi]
No tricky function calls even needed at all.

Community Treasure Hunt

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

Start Hunting!