Filter löschen
Filter löschen

Need help with the code.

1 Ansicht (letzte 30 Tage)
Manav Divekar
Manav Divekar am 12 Nov. 2021
Beantwortet: DGM am 12 Nov. 2021
i want to swaps characters of s as specified by the indices present in the pairs variable. pairs is a 2-column matrix, where on each row, i have given the positions of the two characters that need to swap. eg s = orange & pairs = [1 2]. out expected is roange. what changes you recommend in this code.
function [arr] = shuffletext ( pairs, s)
a = 0;
b = 0;
for i = 1:numel(s)
for j = pairs
s(i) = a;
a = b;
b = s(i);
end
end
arr = b;
Still its not showing any output just a blank if i call the function
using: disp( shuffletext( [1 2], 'orange') )
Output
>> disp( shuffletext( [1 2], 'orange') )
>>

Antworten (1)

DGM
DGM am 12 Nov. 2021
Try this.
shuffletext2([1 2; 3 4; 5 6], 'orange')
ans = 'ronaeg'
function [intext] = shuffletext2(pairs,intext)
for p = 1:size(pairs,1)
intext(pairs(p,:)) = intext(fliplr(pairs(p,:)));
end
end

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by