Clear Filters
Clear Filters

Swaping data from 3 different columns in MATLAB

2 views (last 30 days)
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

Akira Agata
Akira Agata on 12 Aug 2017
Edited: Akira Agata on 12 Aug 2017
If my understanding is correct, the following code will achieve what you want to do.
% Sample 4728x12 numeric array
A = randi([130,140], 4728, 12);
% Find the row where 2nd or 6th or 10th column is 133
idx = any(A(:,[2,6,10]) == 133, 2);
% Then, replace the 2nd, 6th and 10th column of the other rows to 10
A(~idx, [2,6,10]) = 10;
  2 Comments
POKA
POKA on 12 Aug 2017
Edited: POKA on 12 Aug 2017
Thanks for response.My reference column is 2nd column.
But if either in 6th or 10th column 133 is present how to replace it corresponding data of 2nd.
Sir,
It is not working even if it is not finding 133 it is replacing all 2,6,10 column to 10
Akira Agata
Akira Agata on 12 Aug 2017
Hi Poka-san.
First of all, I found typo in my answer, so I've just corrected.
Regarding your additional comment, let me clarify your points.
> My reference column is 2nd column.
If you want to find the rows where 2nd column is 133, then you can do by:
idx = A(:,2) == 133;
But I'm not clearly catching what you want to do for this identified rows.
> if either in 6th or 10th column 133 is present how to replace it corresponding data of 2nd.
This can be done by the following:
idx = any(A(:,[6,10]) == 133, 2);
A(idx,[6,10]) = [A(idx,2), A(idx,2)];
I hope this will help!

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!