Filter löschen
Filter löschen

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

2 Ansichten (letzte 30 Tage)
inci
inci am 27 Mär. 2012
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

Antworten (2)

Thomas
Thomas am 27 Mär. 2012
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 am 27 Mär. 2012
It is much less clumsier than my solution for sure! Many thanks...

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by