How can i manipulate a matrix with this characteristics?

4 views (last 30 days)
Hi, i have a matrix wtih repeated elements like:
[1 4 1 4 2 3 3 5 5 6 6 5]
i want break that matrix. And then in each other matrix, running in a cycle, have only the index referent to each number (1, 2, 3, ...), something like:
1 2 3 4 5 6
[1 4] [2 3] [2 3 [1 4] [3 5 [5 6
3 5] 5 6 6 5]
6 5]
I tried creating a 3 dimension matrix (i,j,k) and each 3rd dimension having above matrix. That hypothesis gives me the repeated solutions inside. I tried to get them out but I might not be able to.
I hope someone can help me out.
PS: Some question that may have, just do it pls.
Regards, Vitor Ribeiro.

Accepted Answer

Joseph Cheng
Joseph Cheng on 9 Apr 2014
Edited: Joseph Cheng on 9 Apr 2014
You can use cells for this. I do not see the pattern you have for [1 4 1 4 2 3 3 5 5 6 6 5] to the broken up matrix. but to set the broken up matrix with index references, cells should work.
MATRIX{1} = [1 4];
MATRIX{2} = [2 3];
MATRIX{3} = [2 3;3 5];
etc.
Which should be simple enough to accomplish in a for loop with whatever initial matrix breakup pattern is.
  1 Comment
Vitor Ribeiro
Vitor Ribeiro on 10 Apr 2014
I have implemented this code, following your ideia.
z=1;
for a=1:size(bus,1)
z=1;
for i=1:size(branch_,1)
for j=1:2
if branch_(i,j)==bus(a)
brx{a,z}=branch_(i,1:2);
z=z+1;
end
end
end
end
Now i need to ensure that in the matrix brx i don't have repeated contents in each row.
[1,4] [1,4] [] [] [] []
[8,2] [8,2] [] [] [] []
[3,6] [3,6] [] [] [] []
[1,4] [1,4] [4,5] [9,4] [4,5] [9,4]
[4,5] [4,5] [5,6] [5,6] [] []
[3,6] [5,6] [5,6] [3,6] [6,7] [6,7]
[6,7] [6,7] [7,8] [7,8] [] []
[8,2] [7,8] [7,8] [8,2] [8,9] [8,9]
[9,4] [8,9] [8,9] [9,4] [] []
Considering that the real aspect of inside matrix brx is represented above. I tried to do a comparison with a conditional structure if. Just like:
if brx{i,j}==brx{i,j+1}
brx{i,j+1}={};
end
But in fact, a comparison between this 2 elements results in a [1 1] matrix, cause it compares each one of the 2 element of inside cells of the matrix brx.
If you know a way to eliminate repeated cells of brx matrix I appreciate you answer me.
I tried below code, and obviously did not result. I have no experience working with cells. Hope someone can help me out.
for i=1:size(brx,1)
for j=1:size(brx,2)-1
for k=1:size(brx,1)
if brx{i,j}==brx{i,k}
brx{i,j+1}={};
end
end
end
end
Best reagards, Vitor Ribeiro.

Sign in to comment.

More Answers (1)

Vitor Ribeiro
Vitor Ribeiro on 10 Apr 2014
I have implemented this code, following your ideia.
z=1;
for a=1:size(bus,1)
z=1;
for i=1:size(branch_,1)
for j=1:2
if branch_(i,j)==bus(a)
brx{a,z}=branch_(i,1:2);
z=z+1;
end
end
end
end
Now i need to ensure that in the matrix brx i don't have repeated contents in each row.
[1,4] [1,4] [] [] [] []
[8,2] [8,2] [] [] [] []
[3,6] [3,6] [] [] [] []
[1,4] [1,4] [4,5] [9,4] [4,5] [9,4]
[4,5] [4,5] [5,6] [5,6] [] []
[3,6] [5,6] [5,6] [3,6] [6,7] [6,7]
[6,7] [6,7] [7,8] [7,8] [] []
[8,2] [7,8] [7,8] [8,2] [8,9] [8,9]
[9,4] [8,9] [8,9] [9,4] [] []
Considering that the real aspect of inside matrix brx is represented above. I tried to do a comparison with a conditional structure if. Just like:
if brx{i,j}==brx{i,j+1}
brx{i,j+1}={};
end
But in fact, a comparison between this 2 elements results in a [1 1] matrix, cause it compares each one of the 2 element of inside cells of the matrix brx.
If you know a way to eliminate repeated cells of brx matrix I appreciate you answer me.
I tried below code, and obviously did not result. I have no experience working with cells. Hope someone can help me out.
for i=1:size(brx,1)
for j=1:size(brx,2)-1
for k=1:size(brx,1)
if brx{i,j}==brx{i,k}
brx{i,j+1}={};
end
end
end
end
Best reagards, Vitor Ribeiro.

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!