make a Sequence number

조회 수: 1 (최근 30일)
NA
NA 2019년 4월 12일
편집: NA 2019년 4월 14일
Layout=[1,1,1;2,2,2;3,2,1;91,3,1;95,4,2;98,4,0.5]; % first column is node, second column is x coordination, third y coordination
A=[1,2;1,3;3,91;91,95;91,98]% edge of graph
I have node 1,2,3,91,95 and 98. I want to change 91,95,98 to 4,5,6.
and also save 91 is 4, 95 is 5 and 98 to 6.
result shloud be
Layout=[1,1,1;2,2,2;3,2,1;4,3,1;5,4,2;6,4,0.5]; % first column is node, second column is x coordination, third y coordination
A=[1,2;1,3;3,4;4,5;4,6]

채택된 답변

Guillaume
Guillaume 2019년 4월 12일
Layout=[1,1,1;2,2,2;3,2,1;91,3,1;95,4,2;98,4,0.5];
A=[1,2;1,3;3,91;91,95;91,98]
searchreplace = [91, 4; 95, 5; 98, 6]; %2 column matrix. First column is value to search, 2nd column is replacement
[toreplace, bywhat] = ismember(Layout(:, 1), searchreplace(:, 1));
Layout(toreplace, 1) = searchreplace(bywhat(toreplace), 2);
[toreplace, bywhat] = ismember(A, searchreplace(:, 1));
A(toreplace) = searchreplace(bywhat(toreplace), 2);
  댓글 수: 1
NA
NA 2019년 4월 14일
편집: NA 2019년 4월 14일
Thank you. I write a code for calculating searchreplace = [91, 4; 95, 5; 98, 6]; automatically.
Is there any possibilities to write this code better?
A=[1,2;1,3;3,91;91,95;91,98];
Layout=[1,1,1;2,2,2;3,2,1;91,3,1;95,4,2;98,4,0.5];
temp_numbering=(1:size(Layout,1))';
temp_Layout=[temp_numbering,Layout];
temp_searchreplace=temp_Layout(:,1:2);
searchreplace=zeros(size(temp_searchreplace));
for i=1:size(temp_searchreplace,1)
if isequal(temp_searchreplace(i,1),temp_searchreplace(i,2))~=1
searchreplace(i,:)=[temp_searchreplace(i,2),temp_searchreplace(i,1)];
end
end
searchreplace(all(searchreplace == 0, 2), :) = []; % searchreplace = [91, 4; 95, 5; 98, 6]; %2 column matrix. First column is value to search, 2nd column is replacement

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by