How to create grid coordinates using elements of two matrices

3 ビュー (過去 30 日間)
Gobert
Gobert 2021 年 10 月 3 日
コメント済み: priyanka kumari 2022 年 5 月 4 日
Can you help? I have a situation like this:
How can I use the elements of the above two matrices to create a new grid coordinate system for another matrix? PS: Only consider a situation like this. Do not think in this sub2ind or ind2sub way.

採用された回答

KSSV
KSSV 2021 年 10 月 3 日
A = [2 3 ; 4 5] ;
B = [6 7 ; 8 9] ;
for i = 1:2
for j = 1:2
P = [A(i,j) B(i,j)]
end
end
P = 1×2
2 6
P = 1×2
3 7
P = 1×2
4 8
P = 1×2
5 9
  2 件のコメント
Gobert
Gobert 2021 年 10 月 3 日
Thank you!
priyanka kumari
priyanka kumari 2022 年 5 月 4 日
Thank you

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 10 月 3 日
Robert:
You can do
M1 = [2 3 ; 4 5];
M2 = [6 7 ; 8 9];
newM = reshape([M1(:), M2(:)], [], 2)
which gives a 4 row by 2 column matrix.
newM =
2 6
4 8
3 7
5 9
Essentially it's a vectorized version of what KSSV did.
To have each element be a 2-by-1 row vector, like you showed, you would need to create either a table or a cell array. I show you how to do this below:
% Create table:
t = table(newM(1:2,:), newM(3:4, :))
% Create cell array
ca = cell(2, 2);
for k = 1 : 4
ca{k} = newM(k,:);
end
ca % Show in command window.
You get:
t =
2×2 table
Var1 Var2
______ ______
2 6 3 7
4 8 5 9
ca =
2×2 cell array
{[2 6]} {[3 7]}
{[4 8]} {[5 9]}

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by