please i need help with Matrix manipulation
古いコメントを表示
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 件のコメント
Walter Roberson
2016 年 1 月 6 日
The 3 4 is not the same length as the other rows so your data array cannot be constructed.
Your second column contains only 1s and 3s, so why do you have more than 2 rows of output? Why is the first row being output? Why does the last row start with 0 0 unlike the others that are filled from the left?
sami elahj
2016 年 1 月 6 日
編集済み: the cyclist
2016 年 1 月 6 日
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.
sami elahj
2016 年 1 月 6 日
編集済み: sami elahj
2016 年 1 月 6 日
採用された回答
その他の回答 (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.
1 件のコメント
sami elahj
2016 年 1 月 6 日
編集済み: sami elahj
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
Hope the attached file gives what you want. Replace matrix data as your data matrix. PS: In the present file I have taken random numbers in data matrix. Some times it may throw error, run the file, it will work.
1 件のコメント
sami elahj
2016 年 1 月 6 日
編集済み: sami elahj
2016 年 1 月 6 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


