Replace rows of matrix with vector

조회 수: 4 (최근 30일)
Jeremy
Jeremy 2014년 7월 15일
댓글: Jeremy 2014년 7월 15일
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
Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 15일
How did you get 'changed' ?
Jeremy
Jeremy 2014년 7월 15일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Joseph Cheng
Joseph Cheng 2014년 7월 15일
편집: Joseph Cheng 2014년 7월 15일
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
Jeremy
Jeremy 2014년 7월 15일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by