combining matricies with common values

3 ビュー (過去 30 日間)
ma sd
ma sd 2020 年 5 月 23 日
編集済み: the cyclist 2020 年 5 月 23 日
i am trying to create a matrix by combining two matricies however, i want to combine them by maching elements from both matricies such as
a=[1 2;
1 4;
5 8]
b=[4 4;
8 9;
2 9]
combined=[4 1;
8 5;
2 1]
i know that this should be simple but i have just started learning matlab and i wasnt able to figure it out
  2 件のコメント
Stephen23
Stephen23 2020 年 5 月 23 日
@ma sd: please explain the rules that you used to "combine" those matrices.
ma sd
ma sd 2020 年 5 月 23 日
i was using the second column form a matched with the first column from b

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

採用された回答

the cyclist
the cyclist 2020 年 5 月 23 日
編集済み: the cyclist 2020 年 5 月 23 日
I'm not certain this is what you want, but this is my best guess:
[tf,idx] = ismember(b(:,1),a(:,2))
complete = [a(idx,2) a(idx,1)];
It seems like you are almost trying to use the fewest words possible to describe your question. Instead, you should try to give a complete, detailed description of what you are trying to do, so that we don't have to guess.
  2 件のコメント
ma sd
ma sd 2020 年 5 月 23 日
i apologize for that what i was trying to explain is hat i have two matricies that i want to merge by matching the second column from a with the first column from b. the resultant i want is a matrix with two columns, the first being the matched value and the second is the first column from a.
the cyclist
the cyclist 2020 年 5 月 23 日
編集済み: the cyclist 2020 年 5 月 23 日
FYI, there are a couple other ways to do what you wanted, after finding the correct sorting. For example
complete = [b(:,1) a(idx,1)];
or
complete = fliplr(a(idx,:));
Since you are new at MATLAB, it is good to learn a few different ways of manipulating arrays.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by