MATLAB Answers

Naime
0

make a Sequence number

Asked by Naime
on 12 Apr 2019
Latest activity Edited by Naime
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]

  0 Comments

Sign in to comment.

1 Answer

Answer by Guillaume
on 12 Apr 2019
 Accepted Answer

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

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.