0

I need to generate a matrix of points given that they meet the condition that at these (x,y) points concentration is greater than 10. Note that I first run a code that gives me concentration at each location c(x,y,t), and now from the results of the first run I need Matlab to "randomly" pick (x,y) points with the above condition. Also, note the dimensions of the results from the first Matlab run (which the random sampling should be based on): concentration changes with location and time and is 52x61x61, x is 1x61, y is 1x52, and time is 1x61.

For example, for a randomly chosen concentration with a value of 50, what is x and y at which this value is observed? I need to do this for 12 different points; 12 different values of c(x,y,t).

I hope my question makes sense. Thanks.

s2015
  • 37
  • 3
  • You should consider adding what you have tried so far to solve the problem – brodoll May 20 '15 at 12:36
  • Do you have an predefined algorithm for computing these concentrations (I guess you mean the density of points) or you'd want the answer to provide this also? –  May 20 '15 at 12:36
  • Concentration or c(x,y,t) is already existing; generated through a different function. The results for c(x,y,t) have dimensions of 52x61x61. What I need now is for Matlab to select random points from the existing c(x,y,t),12, at which concentration meets the condition that is it is greater than 10 at this set of randomly chosen x and y points. – s2015 May 20 '15 at 13:11
  • @s2015 Are there any solutions that work for you? A little feedback would be nice. :-) –  May 20 '15 at 15:21

2 Answers2

0

If I understand your question correctly. Here is a brute force way to do what you want. Hope it helps.

% dimensions
N_X = 52;
N_Y = 61;
N_T = 61;
% space and time vectors
x = linspace(0,1,N_X);
y = linspace(0,1,N_Y);
t = linspace(0,1,N_T);
% set up a grid
[X Y T]         = meshgrid(y,x,t);
% init concentration as uniform random numbers in [0 100]
c_test      = rand(N_X,N_Y,N_T)*100;

% pick concentrations by some condition
condition   = c_test(:) > 99;
% count how many fulfill that condition
N_true      = sum(condition);

if N_true > 0
    % clear terminal for output
    clc
    % now prune the meshgrids and concentration matrices
    % so only the elements that fulfill the condition are true
    X_out = X(condition);
    Y_out = Y(condition);
    T_out = T(condition);
    C_out = c_test(condition);

    for i=1:12
        % pick one of the remaining elements
        % you may want to pick 12 unique elements ? 
        random_idx  = randi(N_true);
        % print to terminal 
        fprintf('c(%3.2f, %3.2f, %3.2f) = %3.1f\n', X_out(random_idx), Y_out(random_idx), T_out(random_idx), C_out(random_idx))
    end
end

I picked higher than 99% to show that the code does not find the correct elements by pure luck :)

Here is the output:

c(0.80, 0.88, 0.47) = 99.4
c(0.30, 0.70, 0.65) = 100.0
c(0.10, 0.62, 0.23) = 99.2
c(0.58, 0.14, 0.37) = 99.1
c(0.25, 0.70, 0.98) = 99.5
c(0.20, 0.46, 0.08) = 99.2
c(0.07, 0.40, 0.05) = 99.5
c(0.05, 0.72, 0.82) = 99.8
c(0.10, 0.90, 0.65) = 99.3
c(0.93, 0.26, 0.22) = 99.5
c(0.05, 0.62, 0.12) = 99.6
c(0.85, 0.80, 0.87) = 99.2
  • Thanks @julietKiloRomeo, I have tried it for the case of my existing concentration, when I run it, I get everything till "N_true". Not sure why the rest is not being executed. – s2015 May 20 '15 at 15:42
  • I have tried it for your case, the concentration profile,etc that you have created to run the code and it runs perfectly fine.@julietKiloRomeo – s2015 May 20 '15 at 15:45
0

Well, since you have the coordinates and the moments x, y, t and the function for concentration c the plan is:

  • calculate the concentration in all points;
  • find where are high concentration points;
  • calculate the indices of those high concentration points;
  • use the indices in the coordinates and moments to get the positions.

The code should look like:

%// Dimensions:
%//   1st --> y
%//   2nd --> x
%//   3rd --> t
conc  = c(x,y,t);
high  = (conc > 50);
indx  = find(high);    

%// Here's the trick: convert back to subscripts the linear indices
%// of the random 12 positions/moments where the concentration was high.
select     = randi(numel(indx), 1, 12);
[yk,xk,tk] = ind2sub(size(high), indx(select));

%// Aggregate the results as (x,y,t) 3x12 numeric array
result = [x(xk); y(yk); t(tk)];
  • Thanks for the response. It did work and looks fine, but is there a way to make it choose numbers at random instead of the first or last 12 points meeting the criterion? for the case of first 12 points, my x is the same while both y and t are changing. It is not really a big deal, but I'm just wondering. @CST-Link – s2015 May 20 '15 at 15:38