필터 지우기
필터 지우기

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

조회 수: 2 (최근 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 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