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

1 回表示 (過去 30 日間)
inci
inci 2012 年 3 月 27 日
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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by