Define new operations for a matrix

5 ビュー (過去 30 日間)
Radu Mihail
Radu Mihail 2020 年 12 月 28 日
コメント済み: Radu Mihail 2020 年 12 月 29 日
I have 2 vectors x=[0 1 2 0 1 0 3 1 0] and its transposed column vector y=x'
I need to define a new operation , let's say * following the multiplication table underneath
0 1 2 3
1 0 3 2
2 3 0 1
3 2 1 0
For instance under this new * commutative operation
0*1=1*0=1
1*1=0 or
3*2=1 or 2*3=1 or
3*3=0
And I would like to build a matrix A having first row x and first column y where the elements of the matrix would be each the result of individual element x.*y or y.*x but using only vectors x and y and defining the new operation * .
The result should be
A=[0 1 2 0 1 0 3 1 0; 1 0 3 1 0 1 2 0 1; 2 3 0 2 3 2 1 3 2; 0 1 2 0 1 0 3 1 0;1 0 3 1 0 1 2 0 1;0 1 2 0 1 0 3 1 0; 3 2 1 3 2 3 0 2 3;1 0 3 1 0 1 2 0 1;0 1 2 0 1 0 3 1 0]
A =
0 1 2 0 1 0 3 1 0
1 0 3 1 0 1 2 0 1
2 3 0 2 3 2 1 3 2
0 1 2 0 1 0 3 1 0
1 0 3 1 0 1 2 0 1
0 1 2 0 1 0 3 1 0
3 2 1 3 2 3 0 2 3
1 0 3 1 0 1 2 0 1
0 1 2 0 1 0 3 1 0

採用された回答

Stephen23
Stephen23 2020 年 12 月 29 日
編集済み: Stephen23 2020 年 12 月 29 日
x = [0,1,2,0,1,0,3,1,0];
y = x.';
V = 0:3; % define value range of multiplication table
M = [0,1,2,3;1,0,3,2;2,3,0,1;3,2,1,0] % define multiplication table
M = 4×4
0 1 2 3 1 0 3 2 2 3 0 1 3 2 1 0
A = interp2(V,V,M,x,y)
A = 9×9
0 1 2 0 1 0 3 1 0 1 0 3 1 0 1 2 0 1 2 3 0 2 3 2 1 3 2 0 1 2 0 1 0 3 1 0 1 0 3 1 0 1 2 0 1 0 1 2 0 1 0 3 1 0 3 2 1 3 2 3 0 2 3 1 0 3 1 0 1 2 0 1 0 1 2 0 1 0 3 1 0
  1 件のコメント
Radu Mihail
Radu Mihail 2020 年 12 月 29 日
Thank you Stephen. This is simple and works perfectly

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 12 月 29 日
You cannot define new operators for the double class, and I would strongly advise you not to try redefining multiplication for double.
You could define your own class and overload operators for that class. Or you could write a function that accepts two double inputs and call it instead of trying to use an operator.
  1 件のコメント
Radu Mihail
Radu Mihail 2020 年 12 月 29 日
I do not know how to write such a function.Would you kindly at least show me a type of function with double inputs to transform the vectors

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by