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

조회 수: 1 (최근 30일)
Mirbahar Naadiya
Mirbahar Naadiya 2017년 11월 9일
댓글: Mirbahar Naadiya 2017년 11월 10일
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에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by