Swaping data from 3 different columns in MATLAB
Show older comments
Hi All
I have a 4728 by 12 matrix consisting of numerical data.I want to swap elements
from column 2,6 and 10 based on condition.
I want to search element 133 in three columns and thus many if and else condition is
coming up.If 133 is not there in any of three column retain 10 in its position. I have done it but it is not efficient because when it was showing error
I was checking the indices and changing the code accordingly . But i want to swap only when the condition is met and by not looking into the array.
My code is like this . In 2nd elseif condition I know 10 is in 6th column and 133 is not present in 2nd
and 10 the column and then I have swap the element of the column. I want to search and
replace from programming.
fmd=fopen('Onlysvid-133.txt','w');
for j=1:length(line)
if(line(j,2)==10 & line(j,6:4:end)~=133)
line(j,2)=line(j,2);
elseif(line(j,2)==133 & line(j,6:4:end)~=133)
line(j,2)=line(j,2);
elseif(line(j,6)==10 & line(j,2:8:end)~=133)
line(j,[2,6])=line(j,[6,2]);
elseif(line(j,2)==10 & line(j,6:4:end)==133)
line(j,[2,10])=line(j,[10,2]);
else(line(j,10)==133 & line(j,2:4:end-1)~=133)
line(j,[2,10])=line(j,[10,2]);
end
fprintf(fmd,'%d\n',line(j,2));
end
fclose(fmd);
% I am failing to implement in below portions of Inputfile contents(4th line)
15 10 0 0 13 10 0 0 14 10 0 0
15 10 0 0 13 10 0 0 14 10 0 0
15 10 0 0 13 10 0 0 14 87 0 0
15 134 0 0 13 10 0 0 14 132 0 0
Thanks
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification 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!