Assign a value to a matrix element defined as a variable

Hi all,
Is it possible to assign values (variables) to elements in a matrix that is defined as a function handle?
For example:
prop1 = zeros(2);
prop1(:,1) = 1;
prop1(:,2) = 2;
prop1
prop1 = 2×2
1 2 1 2
prop2 = @(x) [0 0;
0 0]
prop2 = function_handle with value:
@(x)[0,0;0,0]
prop2(:,2) = @(x) x(2)
Nonscalar arrays of function handles are not allowed; use cell arrays instead.
I would like to assign variables to prop2 as I did to prop1.

2 件のコメント

Geovane Gomes
Geovane Gomes 2023 年 6 月 17 日
編集済み: Geovane Gomes 2023 年 6 月 17 日
Is it possible to write the following function handle with less effort? Maybe with a couple of code lines.
prop = @(x) [x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(1) x(3) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0;
x(2) x(4) 0 0];
Steven Lord
Steven Lord 2023 年 6 月 17 日
Use repmat or repelem.

サインインしてコメントする。

 採用された回答

Torsten
Torsten 2023 年 6 月 17 日
移動済み: Torsten 2023 年 6 月 17 日

1 投票

prop = @(x)[[x(1)*ones(11,1);x(2)*ones(12,1)],[x(3)*ones(11,1);x(4)*ones(12,1)],zeros(23,1),zeros(23,1)];
prop([1 2 3 4])
ans = 23×4
1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0 1 3 0 0

その他の回答 (1 件)

Voss
Voss 2023 年 6 月 17 日

1 投票

prop = @(x)[x([repmat([1 3],11,1); repmat([2 4],12,1)]) zeros(23,2)];

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2023a

質問済み:

2023 年 6 月 17 日

コメント済み:

2023 年 6 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by