make a Sequence number

1 view (last 30 days)
NA
NA on 12 Apr 2019
Edited: NA on 14 Apr 2019
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]

Accepted Answer

Guillaume
Guillaume on 12 Apr 2019
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 Comment
NA
NA on 14 Apr 2019
Edited: NA on 14 Apr 2019
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

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!