Compact way to write matrices containing indices
2 views (last 30 days)
Hi, I have an array A and a matrix B.
(Task 1) First, I would like to check which elements of A are members of the first column of B.
(Task 2) Then, I would like to return (i.e. write down) my matrix B with the same order of the elements of A, and leaving a NaN (or zero) for those elements of A that were not found in B. Is there a compact way to accomplish the Task 2 ?
OK, a little bit messy to explain, but with this example it should be easier to understand what I need:
A = [3
B = [3 5
% Task (1) - Easy
[~,i] = ismember(A,B(:,1))
% Task (2) - First naive attempt gives an error
Index in position 1 is invalid. Array indices must be positive integers or logical values.
% Task (2) - A second naive attempt works, but the 4th row of A is missing
% Desired Output - Here the 4th row of A is present with a NaN in the
% second column
% Is there a compact way to get my desired output?
% Maybe just writing something like: B(?,?)