Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Link the 2th column of a matrix to the first depending on another matrix.

1 回表示 (過去 30 日間)
Klaas
Klaas 2015 年 2 月 9 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello,
I'll just give a simple example to show what I want:
I have a matrix
A=[1,4;
2,5;
3,6];
And a long vector B with values 1,2 and 3 in an undefined order.
B = [1;3;1;2;1;3...];
From A and B I want to make one matrix C in which the first column is vector B and for the second column I want to let the values depend of the value of B. So if the value of B is 1 I want to get a 4, if 2 I want 5, if 3 I want to get 6. So each time the according value defined in matrix A.
Thanks in advance!
  2 件のコメント
Image Analyst
Image Analyst 2015 年 2 月 9 日
You left the answer C out of your example for some reason.
Klaas
Klaas 2015 年 2 月 10 日
C should be: [1,4;3,6;1,4;2,5;1,4;3,6;...]

回答 (2 件)

Image Analyst
Image Analyst 2015 年 2 月 9 日
How about this:
A=[1,4;
2,5;
3,6];
B = [1;3;1;2;1;3];
C = [B, A(B,2)]
C =
1 4
3 6
1 4
2 5
1 4
3 6
  3 件のコメント
Klaas
Klaas 2015 年 2 月 10 日
編集済み: Klaas 2015 年 2 月 10 日
I still have a problem. This works for my example but in my script the numbers 1,2 and 3 from the example are decimal fraction. So I always get the 'Subscript indices must either be real positive integers or logicals.' error.
Someone who has a different solution?
Image Analyst
Image Analyst 2015 年 2 月 10 日
Let's say the number is 2.37. So you want row #2.37 from A. Can you explain exactly what it means to you to get the 2.37'th row from the second column of A?

Roger Stafford
Roger Stafford 2015 年 2 月 10 日
Perhaps what you need is
[~,ix] = ismember(B,A(:,1));
C = A(ix,:);
This assumes that every value in B is to be found somewhere in the first column of A. If not, you will receive an error message.
  1 件のコメント
Image Analyst
Image Analyst 2015 年 2 月 10 日
Oh, is that what he meant? I thought the B was the row numbers of A that he wanted to extract. I guess it's ambiguous but you might be right. Of course having the first column of A be row numbers didn't help clarify it, in fact it made it more ambiguous.

この質問は閉じられています。

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by