Finding any row in an array and replacing with certain new values

2 次查看(过去 30 天)
Hi,
In an array with certain repeated (or unrepeated) row values, I'd like to replace them with new values. i.e.:
a=[1 2 3; 4 5 6; 7 8 9; 1 2 3];
replace [1 2 3]s with [10 11 12]s:
new_a=[10 11 12; 4 5 6; 7 8 9; 10 11 12];
I am working with much bigger arrays and I don't know apriori how many times the row repeates itself.
So currently I am using:
a=[1 2 3; 4 5 6; 7 8 9; 1 2 3];
b=[1 2 3];
k=[10 11 12];
A=find(ismember(a,b,'rows')==1);
[c d] = size(A);
if c > 0
for i = 1:c
a(A(i),:)=k
end
end
But I need to do this operation for dozens of times (and dozens of rows) in a loop where the new values are obtained by "ginput" which makes it very ineffective.
I would so much appreciate any better suggestions.
Many thans,
inci

回答(2 个)

Thomas
Thomas 2012-3-27
Will this help
a=[1 2 3; 4 5 6; 7 8 9; 1 2 3];
for i=1:length(a)
if a(i,:)==[1 2 3]
a(i,:)=[10 11 12]
end
end
for multiple substitutions you can add more if..else's.. Though this might not be the most elegant solution..

inci
inci 2012-3-27
It is much less clumsier than my solution for sure! Many thanks...

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by