Borrar filtros
Borrar filtros

How to make this faster (a bit tricky)

2 visualizaciones (últimos 30 días)
Nick
Nick el 31 de Ag. de 2012
Ok I will try to explain this the best I can. I have a cell array titled "Clusters"
In this example there are 100 cells In Clusters there are 11 rows. In each row there are 3 values.
I need the 3rd value in the row to be the same for any cell that shares the first number.
Example:
Cell 1 has the following:
1_0.5 _ 6
7 _ 0.5 _ 12
28 _ 0.75 _ 20
Cell 2 has:
2 _ 0.5 _ 9
7 _ 0.5 _ 12
21 _ 0.5 _ 4
Cell 3 has:
6 _ 0.25 _ 11
7 _0.5 _ 7
55 _0.25 _19
What I need is a function that will go through and make sure than any rows containing the same first value, share the same 3rd values.
So I want to change these cells to this:
Cell 1 has the following:
1_0.5 _ 6
7 _ 0.5 _ 10
28 _ 0.75 _ 20
Cell 2 has:
2 _ 0.5 _ 9
7 _ 0.5 _ 10
21 _ 0.5 _ 4
Cell 3 has:
6 _ 0.25 _ 11
7 _0.5 _ 10
55 _0.25 _19
This is only going through the example of 7, but you get the idea.
The 3rd values do not have to be unique.
I am allowing the 3rd value to be a random integer between 1 and 20. So if #1 is assigned 5, then all of the cells that contain a 1 in the first column must also have a 5 in the third column.
It is okay if numbers share a 3rd column value.
So all the #1s and #36s might be assigned to a value of 6. That's ok.
I also must run this simulation multiple times.
This is my code:
for BB = 1:MonteCarlos
for i = 1:Points
g = randi(20);
for v = 1:Points
for n = 1:K
if Clusters{v}(n,1) == i
Clusters{v}(n,3) = g;
end
end
end
end
end
BB is how many simulations I had to run. In this situation it's 20, but it doesn't really matter.
I set 1 to equal my total number of cells: 100
V is also equal to 100
K is the number of rows in each cell, 11 in this case.
As the function goes down it first goes through cell i = 1
Then it assigns g to a random integer between 1 and 20
Then it goes through every cell
And through every row
If the first column in that row is equal to our initial cell i
Then the 3rd value is assigned the same g value
So the result is after this is finished, every row in every cell that starts with i will have the same value in the 3rd column.
My trouble is this is too slow as it is.
It takes about 1 second for every simulation. Since the BB is set to 20 it's not so bad. But I will eventually raise this to 500.
So if anyone can see a way to make this faster please let me know.

Respuestas (1)

Walter Roberson
Walter Roberson el 31 de Ag. de 2012
[P, Q, R] = unique( vertcat( vertcat(A{:,1}), vertcat(B{:,1}), vertcat(C{:,1}) ) );
Then, A(:,3) should be assigned num2cell(R(1:size(A,1))), and B(:,3) should be assigned the next size(B,1) entries, and C(:,3) should be assigned the remaining entries.
Caution: you have fractional decimals in your column 1. Those might not compare equal. See http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by