Clear Filters
Clear Filters

i have S 300x800 data set matrix , i need code for replacing coloumns

1 view (last 30 days)
i have S 300x800 data set matrix , i need code for replacing column 120 with 50,and 120 with 121,and 240 with 100 and 240 with 241 ,and 360 with 150 and 360 with 361,.. what is the easy way of doing it with large data set 50 matrix of 300x800 each ?
  2 Comments
Walter Roberson
Walter Roberson on 31 Dec 2017
I notice that you have listed two replacement columns for each column. Are you looking to replace one column each time?
ihab
ihab on 31 Dec 2017
no, i am switching between columns, switch 120 with 50 ,240 with 100, 360 with 150, 200 with 480, 250 with 600

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 31 Dec 2017
I cannot claim that this is completely robust since I’ve experimented only with this code. I appears to do the same operation in one line, without the loops:
M = bsxfun(@times, ones(5,7), (1:7)) % Create Matrix
M(:,[2 5 7]) = M(:, [7 2 5]) % Switch Col#7 With Col#2, Col#2 With Col#5, Col#5 With Col#7
M =
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7
M =
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 2 6 5
1 7 3 4 2 6 5
Experiment with it to see if it does what you want with your matrix.

More Answers (1)

Walter Roberson
Walter Roberson on 31 Dec 2017
to_replace = 120:120:800;
for K = 1 : length(to_replace)
temp = OriginalMatrix;
temp(:,to_replace(K)) = temp(:,K*50);
NewMatrix{2*K-1} = temp;
temp = OriginalMatrix;
temp(:,to_replace(K)) = temp(:,to_replace(K)+1);
NewMatrix{2*K} = temp;
end

Categories

Find more on Characters and Strings 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!