Remove my for loop using meshgrid?

[rows, ~, ~] = size(file)
for i = 1:rows
value = min(abs(file(i,:,1) - shadow(:,:,1)).^2);
storage(i,:) = value;
end
How do i avoid the for loop in this scenario?

9 Kommentare

KALYAN ACHARJYA
KALYAN ACHARJYA am 4 Sep. 2018
Bearbeitet: KALYAN ACHARJYA am 4 Sep. 2018
Can you explain the following statements and more detail?
for 1:rows
file(i,:,1)
Should likely be
for i = 1:rows
Walter Roberson
Walter Roberson am 4 Sep. 2018
After fixing the for loop, that code can only be valid if shadow is a row vector so that the size of file(i,:,1) matches the size of shadow(:,:,1) . With R2016b or later, the subtraction can happen even if the number of rows is different, but the result would be a 2D array that you would not be able to store as a column vector into storage(i,:)
Manne Plok
Manne Plok am 4 Sep. 2018
Bearbeitet: Manne Plok am 4 Sep. 2018
Oh! I forgot to include for i = 1:rows. My bad.
Yes! shadow is a m x 1 x 3 matrix
Manne Plok
Manne Plok am 4 Sep. 2018
Here is a run down of what I want the size of each variable be at the end:
file = m*n*3 3D array
shadow = a*1*3 3D array
storage = m*n 2D array
Any other information?
Walter Roberson
Walter Roberson am 4 Sep. 2018
You subtract shadow(:,:,1) from file(i,:,1), so that is a*1 subtracted from 1 x n x 1. In order for that to work, you would either need a=1 and n=1, or you would need to transpose shadow(:,:,1) so it was 1*a and then you would need a=n .
If you fail to match the size of shadow, then value is either going to fail (R2016a or earlier) or is going to produce an array in which one of the dimensions is a. You are storing that into storage which is m x n. You cannot get that to work unless a is either m or n.
Manne Plok
Manne Plok am 5 Sep. 2018
Bearbeitet: Manne Plok am 5 Sep. 2018
Oh you're right! How about
value = min(abs(file(i,:,1) - shadow(:,:,1)).^2);
That should work? And if yes how do I avoid using the for loop (it ends up slowing down the code heavily)? edit: I also heard bsxfun works too but whenever I try to use it something goes terribly wrong :\
edit2: element wise squaring! .^2
For real values,
min(abs(file(i,:,1) - shadow(:,:,1)).^2)
is the same as
min(abs(file(i,:,1) - shadow(:,:,1))).^2
but the later is more efficient since it only needs to square a single value.
Let's see... 1 x n x 1 minus a x 1 x 1 is an error in R2016a and earlier. In R2016b and later, it will give an a x n result. abs() of that would be a x n. squaring that would be a x n. min() of that would be 1 x n. And that would be acceptable to store into 1 x n in the next row.
To confirm: you want to find the square of the closest distance that exists between each value in file(i,:,1) and all of the values in shadow(:,:,1) ?
Manne Plok
Manne Plok am 5 Sep. 2018
Yes that is exactly right.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Greg
Greg am 5 Sep. 2018
Bearbeitet: Greg am 5 Sep. 2018

0 Stimmen

Given the following code, why are file and shadow 3D? Have we left off an outer loop over dim 3?
[rows, ~, ~] = size(file)
%%%I really don't like "i" as loop index
for irow = 1:rows
value = min(abs(file(irow,:,1) - shadow(:,:,1)).^2);
storage(irow,:) = value;
end
To take advantage of any tricks to avoid the loop, you need another dimension in your data. Note, this requires potentially excessive memory for any speedup you might gain. Assuming you want to do the same thing across the third dimension, try:
shadow = permute(shadow,[4,2,3,1]);
%%%Implicit expansion *will do* multiple singleton dimensions
delta = file - shadow; % R2016b+
% delta = bsxfun(@minus,file,shadow); % R2016a-
storage = min(abs(delta),[],4).^2;
If the third dimension is handled differently, you can use:
shadow_temp = permute(shadow(:,:,1),[2,3,1]);
delta = file - shadow_temp; % R2016b+
% delta = bsxfun(@minus,file,shadow_temp); % R2016a-
storage = min(abs(delta),[],3).^2;
I leave you with 2 thoughts:
  1. Other MVPs encourage the use of bsxfun even in R2016b+. I've never benchmarked it but I trust them when they say it is faster. I personally use implicit expansion for readability. I aim for fast enough code that is easier to read and write versus fastest possible code that coworkers may struggle with.
  2. Depending on your data, you may not need both the abs and .^2. Likely, the squaring accomplishes what you want the abs for, so kill the abs and suck the square inside the min call.

1 Kommentar

Walter Roberson
Walter Roberson am 5 Sep. 2018
More recent tests are not as clear about the relative speeds of bsxfun and implicit expansion. Implicit expansion might possibly be a hair faster in some cases, but the margin of error in the measurements far exceeded the difference in timings.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Gefragt:

am 4 Sep. 2018

Kommentiert:

am 5 Sep. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by