- Find all cases where PCNN_Y_r is 1 (_r is the reshaped vector of the matrix PCNN_Y)
- Skip over the first sp instances of the value 1 (start)
- End after sp+iv_max-1 instances of the value 1 (finish)
Possibility of changing nested for into parfor
1 次查看(过去 30 天)
显示 更早的评论
Hello, i have this piece of code and i'm trying to get rid of nested for and do the following slice of code in parallel. Im also using reshape function to change PCNN_Y and PCNN_S from matrix into a vector. I seem to struggle to understand something, because even though i change the code to work with parfor, it's functionality is missing.
for i = 1:1:x
for j = 1:1:y
if PCNN_Y(i,j) == 1
if position >= sp
PCNN_S(i,j) = bitand(PCNN_S(i,j),254);
PCNN_S(i,j) = bitor(PCNN_S(i,j),stego_bin(iv));
iv = iv + 1;
end
position = position +1;
end
if iv > iv_max
break;
end
end
if iv > iv_max
break;
end
end
I understand that there should now be any return or break statements in parfor loop, but is there a possibility to create a restriction for the iv to not exceed iv_max? Also matrices are 512x512 which means when they are reshaped it is a vector of 1:262144 and mostly because of iv_max i get an error it should not exceed the number of array elements. Index must not exceed something around 780.
0 个评论
回答(1 个)
Raymond Norris
2022-3-8
If I have this right, you're only working with PCNN_Y where its value is 1. Then you skip over the first sp instances where it's 1. Then you want to stop after iv_max occourances past sp.
Below is a way to rewrite your loop.
This way you don't need to end the parfor loop early. You know the exact start and end of the loop.
Below is an example. I've put dummy values for position, iv, and iv_max -- adjust accordingly.
In this case, we'll assign PCCN_S where position is [3 4 5], but nothing after.
2 个评论
Raymond Norris
2022-3-9
Yup. There's an error in my code
ridx= PCNN_Y_r==1;
should be
[~,ridx] = find(PCNN_Y_r==1);
So try this
% Find where the reshaped vector has a value of 1
[~,ridx] = find(PCNN_Y_r==1);
start = ridx(sp);
finish = ridx(sp+iv_max-1);
parfor idx = start:finish
PCNN_S(idx) = bitand();
PCNN_S(idx) = bitor();
end
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!