Replace rows of matrix with vector
Show older comments
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 Comments
Azzi Abdelmalek
on 15 Jul 2014
This is not clear, give a short example, and explain what you want
Joseph Cheng
on 15 Jul 2014
yeah there are some inconsistencies. you say you have a Nx3 but your examples are Nx4.
Azzi Abdelmalek
on 15 Jul 2014
How did you get 'changed' ?
Jeremy
on 15 Jul 2014
Accepted Answer
More Answers (0)
Categories
Find more on Elementary Math 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!