please i need help with Matrix manipulation
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
sami elahj
am 6 Jan. 2016
Beantwortet: sami elahj
am 6 Jan. 2016
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 Kommentare
KSSV
am 6 Jan. 2016
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.
Akzeptierte Antwort
Weitere Antworten (3)
the cyclist
am 6 Jan. 2016
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 Kommentar
KSSV
am 6 Jan. 2016
Bearbeitet: KSSV
am 6 Jan. 2016
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 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!