make a Sequence number

2 ビュー (過去 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 Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by