Updating a matrix based on conditions in other matrixes
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Ulrik William Nash
am 10 Jul. 2017
Bearbeitet: Ulrik William Nash
am 10 Jul. 2017
I have coded the attached loop to update matrix Lr but I would ideally like to use code that can achieve what the shown loop is doing in one sweep (i.e. without needing to loop); the actual matrixes I have can be extremely large.
The code is also shown below, but the layout is off:
k = 1;
pos_neg = [1;1;0];
E = [round(rand(1,3));round(rand(1,3));round(rand(1,3))];
Lr = zeros(numel(pos_neg(:,1)),numel(E(:,1)));
L = 0.5;
for i = 1:numel(E(:,1))
for ii = 1:numel(E(:,1))
if sum(E(i,:)) <= k
if E(i,ii) == 0
Lr(i,ii) = L;
end
if E(i,ii) == 1
Lr(i,ii) = -L;
end
end
if sum(E(i,:)) >= k
if E(i,ii) == 1
Lr(i,ii) = L;
end
if E(i,ii) == 0
Lr(i,ii) = -L;
end
end
end
end
2 Kommentare
Ngonidzashe Nedziwe
am 10 Jul. 2017
hi i would like to help but im confused about what u exactly mean or are trying to achieve when u say in one sweep
Akzeptierte Antwort
Walter Roberson
am 10 Jul. 2017
If I work through the logic correctly,
Lr = L*(1 - 2 * bsxfun( @eq, sum(E,2) <= k, E));
Or if you have R2016b or later,
Lr = L*(1 - 2 * ((sum(E,2) <= k) == E));
4 Kommentare
Weitere Antworten (1)
Jan
am 10 Jul. 2017
Bearbeitet: Jan
am 10 Jul. 2017
Start with cleaning the code by avoiding all repeated calculations inside the loop:
k = 1;
pos_neg = [1;1;0];
E = [round(rand(1,3));round(rand(1,3));round(rand(1,3))];
Lr = zeros(numel(pos_neg(:,1)),numel(E(:,1)));
L = 0.5;
sE = size(E, 1);
for i = 1:sE
sumEi = sum(E(i,:));
for ii = 1:sE
if sumEi <= k
if E(i,ii) == 0
Lr(i,ii) = L;
end
if E(i,ii) == 1
Lr(i,ii) = -L;
end
end
if sumEi >= k
if E(i,ii) == 1
Lr(i,ii) = L;
end
if E(i,ii) == 0
Lr(i,ii) = -L;
end
end
end
end
Now you see that the "if sumEi <= k" test does not depend on the "for ii" loop. Then move it outside this loop:
sE = size(E, 1);
for i = 1:sE
sumEi = sum(E(i,:));
if sumEi <= k
for ii = 1:sE
if E(i,ii) == 0
Lr(i,ii) = L;
elseif E(i,ii) == 1
Lr(i,ii) = -L;
end
end
elseif sumEi >= k
for ii = 1:sE
if E(i,ii) == 1
Lr(i,ii) = L;
elseif E(i,ii) == 0
Lr(i,ii) = -L;
end
end
end
end
Now see, shat you want to achieve actually (this should be contained as a clear and exhaustive comment in the code): Lr is set to L or -L. So start with set all elements to L and swap the sign for the specific elements:
Lr = repmat(L size(pos_neg, 1), size(E,1));
size(E, 1) is more efficient than numel(E(:,1)), because the latter creates a temporary vector only to measure its length.
sumEi = sum(E, 1);
index = ((sumEi <= k) & (E == 1)) | ...
(sumEi >= k) & (E == 0)); % >= R2016b
Note that sumEi == k belongs to both cases. I assume one of them should be a > or < .
If the values of E are either 1 or 0, this can be expressed in one logical comparison only. I cannot test this currently, but it is something like:#
Lr = repmat(L, size(pos_neg, 1), size(E,1));
index = xor(sum(E, 1) <= k, E == 1); % Or E==0? Or < instead of <= ? Try this by your own
Lr(index) = -L;
2 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!