How to add a constant value above a matrix in following code?

3 ビュー (過去 30 日間)
Nisar Ahmed
Nisar Ahmed 2022 年 11 月 7 日
回答済み: Steven Lord 2022 年 11 月 7 日
Hello
P is a matrix of 100 106 and I am trying to add 20 constant values (say 0.25) above each row and 20 constant values (say 0.21) below each rows to make P as 140 106. How I can change following code? instead of taking first value of each column (P(1,:)), how I can replace with a constant value?
A_pad = zeros(140,106);
A_pad(1:20,:) = ones(20,1)*P(1,:);
A_pad(21:120,:) = P;
A_pad(121:140,:) = ones(20,1)*P(end,:);

採用された回答

Torsten
Torsten 2022 年 11 月 7 日
P = [0.25*ones(20,106);P;0.21*ones(20,106)]

その他の回答 (1 件)

Steven Lord
Steven Lord 2022 年 11 月 7 日
A = magic(4)
A = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
B = [repmat(-999, 2, width(A));
A;
repmat(Inf, 3, width(A))]
B = 9×4
-999 -999 -999 -999 -999 -999 -999 -999 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf
This pads 2 rows containing the constant -999 above the matrix A and 3 rows containing the constant Inf below.

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by