please i need help with Matrix manipulation
조회 수: 2 (최근 30일)
이전 댓글 표시
hello everyone, i need help on writing a script that does the following:
data=[1 1 2;
2 1 3;
3 3 4;
3 3 5;
4 3 6]
the script need to check if a number in the second column is repeated ,means its a layer , for example 1 2 and 1 3 will be layer 1 containing 2 and 3 etc... then the final result should be something like this
result=[1 0 0 0 0;
2 3 0 0 0;
0 0 4 5 6]
any suggestions?
thank you
댓글 수: 4
KSSV
2016년 1월 6일
Hi
result matrix is not bit clear. You want layers to be filled depending on its values? I mean there is layer 1 and 3. Do you want layer 2 to be completely zero? You need to elaborate result matrix.
채택된 답변
추가 답변 (3개)
the cyclist
2016년 1월 6일
I think I kinda understand what you mean, but there is still too much guesswork here.
If your result were
result=[2 3 0 0 0;
0 0 4 5 6]
then I would understand how you got it. But where does your first row
result=[1 0 0 0 0 ...
come from? How do we get that?
And why is the result not
result=[1 0 0 0 0 0;
0 2 3 0 0 0;
0 0 0 4 5 6]
with the 2nd row offset in the same way that the first one is?
Your one example does not adequately explain the rule/algorithm for going from data to result.
KSSV
2016년 1월 6일
편집: KSSV
2016년 1월 6일
Hi sami elahj
You can follow the below code to find the repetitions, which gives you layers. Here I have generated random numbers to create data. You can replace data with your matrix.
N = 10 ;
a = 1 ; b = 20 ;
row1 = linspace(1,N,N)' ;
row2 = round(a + (b-a).*rand(N,1)) ;
row3 = round(a + (b-a).*rand(N,1)) ;
data = [row1 row2 row3] ;
% check for repetition
eps = 1.e-5 ;
repeat = cell(N,1) ;
repeat_val = cell(N,1) ;
for i = 1:N
diff_row2 = repmat(data(i,2),[N,1])-data(:,2) ;
% Arrange the distances in ascending order
[val, pos] = sort(abs(diff_row2)) ;
% Pick the points which are repeated
repeat{i} = pos(val<=eps) ;
repeat_val{i} = data(repeat{i},2) ;
end
repeat, repeat_val gives you the indices and values respectively. Coming about the result matrix. You have to clear few questions.
Regards
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!