Rows and Columns manipulation

7 views (last 30 days)
Adnan Khan
Adnan Khan on 16 Mar 2021
Commented: Adnan Khan on 17 Mar 2021
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.

Accepted Answer

Matt J
Matt J on 16 Mar 2021
Edited: Matt J on 16 Mar 2021
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 Comments
Adnan Khan
Adnan Khan on 16 Mar 2021
Thanks a bunch Sir.
Adnan Khan
Adnan Khan on 17 Mar 2021
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.

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!