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

1 vue (au cours des 30 derniers jours)
ihab
ihab le 31 Déc 2017
Commenté : Star Strider le 31 Déc 2017
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 commentaires
Walter Roberson
Walter Roberson le 31 Déc 2017
I notice that you have listed two replacement columns for each column. Are you looking to replace one column each time?
ihab
ihab le 31 Déc 2017
no, i am switching between columns, switch 120 with 50 ,240 with 100, 360 with 150, 200 with 480, 250 with 600

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 31 Déc 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.
  2 commentaires
ihab
ihab le 31 Déc 2017
thank you very much, i really appreciate it
Star Strider
Star Strider le 31 Déc 2017
As always, my pleasure.

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 31 Déc 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

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by