how to search a value of a matrix and replace by vector value?

1 次查看(过去 30 天)
hello everyone, it would be very kind of you if someone could help me to figure it out. i want to replace elements of matrix, the matrix contains student IDs.
if true
% code
User1 User2
84599 84607
151791 152219
151791 152195
357518 357809
357518 357863
end
the total number of users in the mentioned data is 8, one users may have many records in data.
if true
% code
84599 1
84607 2
151791 3
152195 4
152219 5
357518 6
357809 7
357863 8
end
I want to find one value like "84599" in all data and replace it by "1", then "84607" replace it by "2". The below result is the desired one
if true
% code
1 2
3 4
5 6
7 8
6 8
end
Thanks in advance
  1 个评论
Rik
Rik 2017-11-9
The naive approach would be a for-loop, but if you take a look at ismember, you might be able to find a method of doing in one go with clever indexing.

请先登录,再进行评论。

采纳的回答

Eric
Eric 2017-11-9
studentIDs = [84599 84607; ...
151791 152219; ...
151791 152195; ...
357518 357809; ...
357518 357863];
ID2code = [84599 1; ...
84607 2; ...
151791 3; ...
152195 4; ...
152219 5; ...
357518 6; ...
357809 7; ...
357863 8];
output = studentIDs;
for i=1:size(ID2code,1)
output(studentIDs==ID2code(i,1)) = ID2code(i,2);
end
The simple, straightforward approach. There may be more efficient ways, but this should get the job done.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by