Replace rows of matrix with vector

4 vues (au cours des 30 derniers jours)
Jeremy
Jeremy le 15 Juil 2014
Commenté : Jeremy le 15 Juil 2014
So basically what I've done is create a function that allows me to replace a row within a matrix with a specified vector:
function [replacedM] = replace row(M,row,replace)
r=length(M);
for i=1:r
if M(i,:)==row
M(i,:)=replace;
end
end
replacedM=M;
end
Now, I have 2 matrices about 13260X3 in size. One is called prechanged, the other is called changed (where row m of 'changed' is the modified row m of 'prechanged.') My main matrix, 'main,' is about about 76000X3 in size. Multiple rows of 'main' have triplicate rows.
For example, 'main' looks something like:
1 2 3 1
2 8 1 0
1 2 3 1
2 0 8 2
3 0 8 8
1 2 3 1
0 0 1 2
Rows 1, 3 , and 6 all have the same values.
'prechange' would look something like:
0 0 1 2
1 2 3 1
2 0 8 2
2 8 1 0
3 0 8 8
'changed' would look something like:
0 0 2 4
2 4 6 2
4 0 4 4
4 4 2 0
6 0 4 4
I'm implementing the replacerow function on 'main' like this:
for j=1:length(pre)
main=replacerow(main,prechange(j,:),changed(j,:));
end
BUT it takes about 10 minutes (maybe because all main, prechange, and changed are extremely large matrices). Would anyone know if there is a way to shortening computational time? THANKS!
  4 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 15 Juil 2014
How did you get 'changed' ?
Jeremy
Jeremy le 15 Juil 2014
My apologies. Just take out the last column. I made these matrices on the spot. So I have three matrices: main, prechanged, and changed. main is the original 76000X3 matrix with triplicates of rows, in no order. These rows represent vertices for strings of hexagonal shapes.
I used the unique function to remove the triplicates. With this new matrix of about 13260X3, I managed to group the 2210 hexagons, such that every group of 6 rows represented the vertices to a hexagon. This newly ordered matrix is prechanged. Then I found each hexagon's respective centroids in order to transform the hexagon by some factor k. (So each row of changed is just a modified row of prechanged - their row indices are the same if that makes sense.)
The problem is that I need to put these modified values back into the main matrix.

Connectez-vous pour commenter.

Réponse acceptée

Joseph Cheng
Joseph Cheng le 15 Juil 2014
Modifié(e) : Joseph Cheng le 15 Juil 2014
well here is a thing that you could do to speed it up.
function [M] = replace(M,row,replace)
[~,ind]=ismember(M,row,'rows');
ind = find(ind==1);
M(ind,:) = repmat(replace,length(ind),1);
end
and I tested it out using this
X = randi(10,76000,3);
prechange = randi(10,1000,3);
changed = randi(10,1000,3);
for i =1:length(prechange)
rowsdupe = randi(76000,1,randi(10000,1,1));
X(rowsdupe,:) = repmat(prechange(i,:),length(rowsdupe),1);
end
h=waitbar(0,'waiting')
tic
for j=1:length(prechange)
X=replace(X,prechange(j,:),changed(j,:));
waitbar(j/length(prechange),h,['performing step: ' num2str(j) '/' num2str(length(prechange))]);
end
close(h)
toc
elapsed time = 32.99 seconds.
  1 commentaire
Jeremy
Jeremy le 15 Juil 2014
Thanks! That seemed to help shorten the computational time by 2:30 minutes! I'll try to see what else might be causing the problem.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Elementary Math 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