please i need help with Matrix manipulation

3 次查看(过去 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
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
sami elahj 2016-1-6
编辑:sami elahj 2016-1-6
hi Dr. Siva For each repeated value in the second column, I'd like to all values in the third column that share each repeated value in the second column as entries in the correct column. (from the pic)
1--->2
1--->3
so layer 1 will contain 2 and 3
3--->4
3--->5
3--->6
layer 2 will contain the 4 , 5 and 6
resulting in
[2 3 0 0 0;
0 0 4 5 6]
or transposed
once layer is filled with the corresponding values the rest of the values should be zeros

请先登录,再进行评论。

采纳的回答

sami elahj
sami elahj 2016-1-6
it seems like this works
mx=max(data(:,3)); %// maximal node index
ly = zeros(mx,1);
for ii=2:mx,
ly(ii) = ly( data( data(:,3)==ii, 2 ) )+1; %// get the layer of the parent
end
for ci=1:max(ly),
c{ci} = find(ly==ci); %// put all nodes of layer ci in a cell
end;
mxc = max(cellfun(@numel,c));
res=zeros(mxc,max(ly));
for ci=1:numel(c),
res(1:numel(c{ci}),ci)=c{ci}(:);
end;

更多回答(3 个)

the cyclist
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
sami elahj 2016-1-6
编辑:sami elahj 2016-1-6
Thank you the cyclist, i apologize for not explaining accurately, in the example i ve attached comes a tree form data where from each ramification (root=the repeated number in the data matrix) comes out a number of lines and points. the first column [1 0 0 0 0 0] its a mistake of mine so it can be ignored, and the wanted result can rather the way i implemented it (as columns) or the way you suggested(as rows), en both way columns/rows to me they ll be meaning a layer. one more thing the resulting matrix dimensions are not important once the data is classified as asked. hoping i explained it better this time :)

请先登录,再进行评论。


KSSV
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

KSSV
KSSV 2016-1-6
编辑:KSSV 2016-1-6
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
sami elahj 2016-1-6
编辑:sami elahj 2016-1-6
hey Dr Siva thanx for the script, i have try it and this is what i got:
ans =
1 2 3 0 0 0
3 0 0 4 5 6
the problem that occurs is when trying to expand the data matrix (as putting different possibilities) for example
data = [1 1 2;
2 1 3;
3 2 4;
4 3 5;
5 3 6;
6 4 7;
7 5 8]
will be equivalent on the sketch as shown, so basically the problem when there is no repeated value in the second column,say the 2--->4 example, the script will need to check if number 2 belongs to the previous layer if so whatever numbers comes from it will belong to the next layer. i don't know if that make sense to you? thanx
Ps: this is the original system i am trying to work out, in school they recommended to use a simple loop to solve this but i don't seem to figure out the best logic as i was getting errors all the time

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by