Rows and Columns manipulation

9 ビュー (過去 30 日間)
Adnan Khan
Adnan Khan 2021 年 3 月 16 日
コメント済み: Adnan Khan 2021 年 3 月 17 日
Hey everyone. I have kind of tricky problem to solve.
I have three matrices, A with size of 1x1001 and is unique,
B is of size 1x119 (not unique) and
C is of size 1x1001 and is unique.
What I want is to see whether elements of B exists in C (repititions to be considered), if yes then they are at which indices in C? For example B(1,1) upto B(1,119) needs to be compared with the enitre C. All the values of B that exists in C are then stored in another matrix D along with the index of C. Here index is nothing but the number of column in which it occured, since there is only a single row.
Matrix D will contain the common value and the index at which it occured in C. Note: The values of matrices B and C needs to be compared upto two decimal places and ignore rest of it. For example value 50.234 in B is considered equal to value 50.239 in C. Here we will store 50.23 in matrix D along with its index at which it occured in C.
Now the final part,
Since D has a pair (value, index). Pick D's value and mulitply it with the value occuring at this index (specific number of column) in A. Do it for all values of D and store the result in E.
For example, if D(1,1) = (50.23, 4). Mulitply 50.23 with the value of A in 4th column.

採用された回答

Matt J
Matt J 2021 年 3 月 16 日
編集済み: Matt J 2021 年 3 月 16 日
fn=@(z) floor(z*100)/100;
B=fn(B);
C=fn(C);
[~,index]=ismember(B, C);
index=nonzeros(index);
D={C(index), index};
E=D{1}.*A(index);
  2 件のコメント
Adnan Khan
Adnan Khan 2021 年 3 月 16 日
Thanks a bunch Sir.
Adnan Khan
Adnan Khan 2021 年 3 月 17 日
Sir one more question, what if B was 119x1 instead of 1x119. Will there be any change in the code? Looking forward to your kind response.

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by