Finding any row in an array and replacing with certain new values
조회 수: 1 (최근 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
댓글 수: 0
답변 (2개)
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..
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!