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!