Matlab code to analyze data on a grid - matlab

I have a point set with (x,y) coordinates and their corresponding weights in matrix a where the 1st, 2nd and 3rd columns are x, y, and weight respectively. I want to divide this point set into grid cells, and count the number of points in each grid and the total weight of each grid.
I tried the small example below, but it did not work. Here I tried to divide this data set into a 2x2 small grid and tried to count number of points and their sum of weights. Further, I have big data set, so I can not extend this approach further when I need different step sizes for grid.
Can someone please help me to develop an easier approach?
function dataTree
count=zeros(9,1);
avg=zeros(9,1);
data=[1 3 100; 2 1 120; 3 5 110; 4 2 100; 5 3 150; 6 2 100];
for i=1:6
if data(i,1)<=2
for j=1:6
if data(j,2)<=2
count(1) = count(1) + 1;
avg(1) = avg(1) + data(j,3);
elseif data(j,2)<=4
count(2) = count(2) + 1;
avg(2) = avg(2) + data(j,3);
elseif data(j,2)<=6
count(3) = count(3) + 1;
avg(3) = avg(3) + data(j,3);
end
end
elseif data(i,1)<=4
for j=1:6
if data(j,2)<=2
count(4) = count(4) + 1;
avg(4) = avg(4) + data(j,3);
elseif data(j,2)<=4
count(5) = count(5) + 1;
avg(5) = avg(5) + data(j,3);
elseif data(j,2)<=6
count(6) = count(6) + 1;
avg(6) = avg(6) + data(j,3);
end
end
elseif data(i,1)<=6
for j=1:6
if data(j,2)<=2
count(7) = count(7) + 1;
avg(7) = avg(7) + data(j,3);
elseif data(j,2)<=4
count(8) = count(8) + 1;
avg(8) = avg(8) + data(j,3);
elseif data(j,2)<=6
count(9) = count(9) + 1;
avg(9) = avg(9) + data(j,3);
end
end
end
end
count'
avg'

If your x and y are not yet rounded to some arbitrary units, do so first:
x = round((x - min(x))/edgelength+1);
this makes sure that you obtain grid with edgelength sized squares, which is indicated by non-zero integers. Do the same for y.
Then you can use either sparse or accumarray to get the total weight. sparse is faster, but is less wide applicable:
gridWeight = sparse(x,y,weight);
if you want to get average weights, add 1 for each entry and divide by that matrix:
NumEntries = sparse(x,y,1);
MeanWeights = gridWeight./NumEntries;
accumarray can do both of these operations in one go:
gridWeight = accumarray([x y],weight);
MeanWeights = accumarray([x y], weight,[],#mean); %//add ,[], 'issparse' for sparse matrix
Note that sparse is a sub-functionality of accumarary by setting accumarray=([i,j],val,[],#sum,[],'issparse'). The only function sparse can handle is #sum and it's sole fill-value is 0, whilst for accumarray other functions and values can be used.

Related

Creating a matrix of equations for counter phased flow

I am modelling flow through a tube in tube heat exchanger using MATLAB using the nodal port method. I need to populate a matrix with differential equations in order to solve for the enthalpies at each point. I have divided my pipe into multiple sections, each with 3 nodes. 1 node for the inner fluid, 1 node for the outer fluid, and 1 node for the pipe. However, due to the fact that there is counter flow I need to populate the matrix in a way that the outer fluid counts in reverse to the inner fluid. For example, if I have 9 nodes, my column vector will be [Eq 1 Eq 2 Eq 9 Eq 4 Eq 5 Eq6 Eq 7 Eq 8 Eq 3]. My code seems to work, but it doesn't enter anything for the 3rd position of the vector. Thank you in advance for the help.
NXP = 5; %Number of Divisions
HX = zeros(NXP,1);
cntr=2; %To see which number equation is being input
for j=1:NXP
if j==1
HX(1+(j-1)*3,1) = 125; %Boundary Condition
else
HX(1+(j-1)*3,1) = cntr;
cntr = cntr+1;
end
HX(2+(j-1)*3,1) = cntr;
cntr = cntr+1;
if j==NXP
HX(3+(j-1)*3,1) = 40; %Boundary Condition
else
HX(3*NXP-3*(j-1),1) = cntr;
cntr = cntr+1;
end
end
'''
I changed a bit your attempt, most notably moving the boundary conditions outside of the loop, but it now returns what I guess you were looking for:
NXP = 5;
HX = zeros(3*NXP, 1);
for j = 1:NXP
idx = (j - 1)*3;
HX(idx + 1, 1) = idx + 1;
HX(idx + 2, 1) = idx + 2;
HX(length(HX) - idx, 1) = idx + 3;
end
% Boundary conditions
HX(1) = 125;
HX(end) = 40;
EDIT:
Even better, just initialize the array as
HX = (1:3*NXP)';
and then just change the order for your 3rd nodes
HX(3:3:length(HX)) = flipud(HX(3:3:length(HX)));
and finally set your boundary conditions
HX(1) = 125;
HX(end) = 40;

Seeking advice on trying to read a moore neighbourhood for a 2D cellular automata in MATLAB for an epidemic simulator

I'm currently working on a code that makes use of a 2D cellular automata as an epidemic simulator in MATLAB. The main basic rule I'm trying to implement is that if any neighbour within a Moore Neighbourhood with a 1-cell radius is infected, the cell will become infected. But I can't seem to get a good code working for it.
Basically what I'm trying to do is say with for a cell with a one cell radius Moore neighbourhood, if any values in this neighbourhood = 2, then the initial cell will become 2.
I've tried using the forest fire code on the rosetta code as a basis for my code behaviour but it doesnt work very well. The rules don't really work that well when applying it to mine. I've tried using the mod function and a series of if loops to attach. I'll put in some code of each to give context.
This example doesn't really function well as an epidemic simulator to be honest.
matlab
clear; clc;
n = 200;
N = n/2;
E = 0.001; % Creating an arbitrary number for population exposed to
the disease but not infected
p = 1 + (rand(n,n)<E);
%p = ceil(rand(n,n)*2.12) - 1;
% ratio0 = sum(p(:)==0)/n^2;
% ratio1 = sum(p(:)==1)/n^2;
% ratio2 = sum(p(:)==2)/n^2;
% ratio3 = sum(p(:)==3)/n^2;
S = ones(3); S(2,2) = 0;
ff = 0.00000000002;
p(N,N) = 3;
%% Running the simulation for a set number of loops
colormap([1,1,1;1,0,1;1,0,0]); %Setting colourmap to Green, red and
grey
count = 0;
while(count<365) % Running the simulation with limited number of runs
count = count + 1;
image(p); pause(0.1); % Creating an image of the model
P = (p==1); % Adding empty cells to new array
P = P + (p==2).*((filter2(S,p==3)>0) + (rand(n,n)<ff) + 2); % Setting
2 as a tree, ignites based on proximity of trees and random
chance ff
P = P + (p==3); % Setting 3 as a burning tree, that becomes 1,
p = P;
end
second idea. this basically returns nothing
matlab
clear;clf;clc;
n = 200;
pos = mod((1:n),n) + 1; neg = mod((1:n)-2,n) + 1;
p = (ceil(rand(n,n)*1.0005));
for t = 1:365
if p(neg,neg) ==2
p(:,:) = 2;
end
if p(:,neg)==2
p(:,:) = 2;
end
if p(pos,neg)==2
p(:,:) = 2;
end
if p(neg,:)==2
p(:,:) = 2;
end
if p(pos,:)==2
p(:,:) = 2;
end
if p(neg,pos)==2
p(:,:) = 2;
end
if p(:,pos)==2
p(:,:) = 2;
end
if p(pos,pos)== 2
p(:,:) = 2;
end
image(p)
colormap([1,1,1;1,0,1])
end
third I tried using logic gates to see if that would work. I don't know if commas would work instead.
matlab
clear;clf;clc;
n = 200;
pos = mod((1:n),n) + 1; neg = mod((1:n)-2,n) + 1;
p = (ceil(rand(n,n)*1.0005));
%P = p(neg,neg) + p(:,neg) + p(pos,neg) + p(neg,:) + p(:,:) + p(pos,:)
+ p(neg,pos) + p(:,pos) + p(pos,pos)
for t=1:365
if p(neg,neg)|| p(:,neg) || p(pos,neg) || p(neg,:) || p(pos,:) ||
p(neg,pos) || p(:,pos) || p(pos,pos) == 2
p(:,:) = 2;
end
image(p)
colormap([1,1,1;1,0,1])
end
I expected the matrix to just gradually become more magenta but nothing happens in the second one. I get this error for the third.
"Operands to the || and && operators must be convertible to logical scalar values."
I just have no idea what to do!
Cells do not heal
I assume that
Infected is 2, non-infected is 1;
An infected cell remains infected;
A non-infected cell becomes infected if any neighbour is.
A simple way to achieve this is using 2-D convolution:
n = 200;
p = (ceil(rand(n,n)*1.0005));
neighbourhood = [1 1 1; 1 1 1; 1 1 1]; % Moore plus own cell
for t = 1:356
p = (conv2(p-1, neighbourhood, 'same')>0) + 1; % update
image(p), axis equal, axis tight, colormap([.4 .4 .5; .8 0 0]), pause(.1) % plot
end
Cells heal after a specified time
To model this, it is better to use 0 for a non-infected cell and a positive integer for an infected cell, which indicated how long it has been infected.
A cell heals after it has been infected for a specified number of iterations (but can immediately become infeced again...)
The code uses convolution, as the previous one, but now already infected cells need to be dealt with separately from newly infected cells, and so a true Moore neighbourhood is used.
n = 200;
p = (ceil(rand(n,n)*1.0005))-1; % 0: non-infected. 1: just infected
T = 20; % time to heal
neighbourhood = [1 1 1; 1 0 1; 1 1 1]; % Moore
for t = 1:356
already_infected = p>0; % logical index
p(already_infected) = p(already_infected)+1; % increase time count for infected
newly_infected = conv2(p>0, neighbourhood, 'same')>0; % logical index
p(newly_infected & ~already_infected) = 1; % these just became infected
newly_healed = p==T; % logical index
p(newly_healed) = 0; % these are just healed
image(p>0), axis equal, axis tight, colormap([.4 .4 .5; .8 0 0]), pause(.1) % plot
% infected / non-infected state
end

Matlab - Generating random numbers and counting

I need to create an experiment where I generate random numbers from 1 to 4 and stop when the numbers 1,2,3,4 appear at least once. Then I need to display the amount of random numbers.
I'm new to mathlab and so far I only have a vague idea on how to do this.
I think I need to create a counter for each number and stop when each one is atleast one.
y=randi([1,4]);
disp(y);
Generates 1 random number but I think I need to use a while loop to keep generating random numbers and break it when the counters are all >=1
Thanks
I think the following code will solve you problem.
counter = zeros(4, 1);
while ismember(0, counter)
y = randi([1, 4]);
counter(y) = counter(y)+1;
end
Counter contains the count for each number.
Yang Zhang's ^^ is much better
That solution will work! You could also use a vector to store the counters
X = zeros(1, 4);
count = 0;
while X(1) == 0 || X(2) == 0 || X(3) == 0 || X(4) == 0
r = randi([1,4])
if r == 1
X(1) = X(1) + 1;
elseif r == 2
X(2) = X(2) + 1;
elseif r == 3
X(3) = X(3) + 1;
elseif r == 4
X(4) = X(4) + 1;
end
count = count + 1;
end
disp(count)
there is probably a more elegant way to do this, but it is a small enough problem that efficiency is not an big issue

Gauss-Seidel code not converging on solution

I am unable to get converging values using a Gauss-Seidel algorithm
Here is the code:
A = [12 3 -5 2
1 6 3 1
3 7 13 -1
-1 2 -1 7];
b = [2
-3
10
-11];
ep = 1e-8;
[m, n] = size(A);
[n, p] = size(b);
x = zeros(n, 1001);
x(:, 1) = []
for k=0:1000
ka = k + 1;
if ka == 1001
break;
end
xnew = zeros(n,1);
for i=1:n
sum = 0;
j = 1;
while j < i
s1 = s1 + A(i,j) * x(j, ka + 1);
j = j + 1;
end
j = i + 1;
while j <= n
sum = sum + A(i,j) * x(j, ka);
j = j + 1;
end
xnew(i) = (b(i) - sum) / A(i, i);
% if result is within error bounds exit loop
if norm(b - A * xnew, 2) < ep * norm(b, 2)
'ending'
break
end
end
x(:,ka + 1) = xnew;
end
I cannot get the A * xnew to converge on b what am I doing wrong?
I have tried running this changing the syntax several times, but I keep getting values that are way off.
Thanks!
Gabe
You have basically two problems with your code:
(1) You are using two different variables "sum" and "s1". I replaced it by mySum. By the way, dont use "sum", since there is a matlab function with this name.
(2) I think there is also a problem in the update of x;
I solved this problem and I also tried to improve your code:
(1) You dont need to save all "x"s;
(2) It is better to use a "while" than a for when you dont know how many iterations you need.
(3) It is good to use "clear all" and "close all" in general in order to keep your workspace. Sometimes old computations may generate errors. For instance, when you use matrices with different sizes and the same name.
(4) It is better to use dot/comma to separate the lines of the matrices
You still can improve this code:
(1) You can test if A is square and if it satisfies the conditions necessary to use this numerical method: to be positive definite or to be diagonally dominant.
clear all
close all
A = [12 3 -5 2;
1 6 3 1;
3 7 13 -1;
-1 2 -1 7];
b = [2;
-3;
10;
-11];
ep = 1e-8;
n = length(b); % Note this method only works for A(n,n)
xNew=zeros(n,1);
xOld=zeros(n,1);
leave=false;
while(~leave)
xOld=xNew;
for i=1:n
mySum = 0;
j = i + 1;
while j <= n
mySum = mySum + A(i,j) * xOld(j,1);
j = j + 1;
end
j = 1;
while j < i
mySum = mySum + A(i,j) * xNew(j,1);
j = j + 1;
end
mySum=b(i,1)-mySum;
xNew(i,1) = mySum / A(i, i);
end
if (norm(b - A * xNew, 2) < ep * norm(b, 2))
disp('ending');
leave=true;
end
xOld = xNew;
end
xNew

How to distribute values randomly over a given time period?

I am trying to distribute a certain value over a random period of time. To clarify more ,
Suppose I want to distribute product x and y over 30 days. I have 1500 items of product x that has to be distributed over 30 days randomly. There is a restriction on the number of items that can be distributed over 1 day i.e.max 60.
I have been trying to scratch out something but am really unsucessful with this problem. I am really new to programming so it would be a real help if somebody could point me to the right approach.
As an addendum, if I have more than 1 items to be distributed (like suppose there are x,y and z) with different values (ex. 1500, 1000, 900) and there is a limitation on how many items can be distributed on a particular day (max 150 per day) will this logic still work or should I look at something new. Also, should there be a check, like suppose 100 of x, 20 of y and 30 of z are distributed, then subtract the value (for the next day I have 1400 of x, 980 of y and 870 of z available for distribution) as this will change the permutation values ?
Thank you guys !
This should work for you!
days = 30;
elem = 1500;
max_x = 60;
x = randi(max_x,days,1);
remain = elem - sum(x);
while remain > 0
idx_1 = find(x < max_x); % Numbers that can be increased
idx_fill = randperm(numel(idx_1),remain);
% idx_fill = idx_fill(:,1); % Might be needed
x(idx_1(idx_fill)) = x(idx_1(idx_fill)) + 1;
remain = elem - sum(x);
end
while remain < 0
idx_2 = find(x > 0); % Numbers that can be reduced
idx_red = randperm(numel(idx_2),abs(remain));
% idx_red = idx_red(:,1); % Might be needed
x(idx_2(idx_red)) = x(idx_2(idx_red)) - 1;
remain = elem - sum(x);
end
sum(x)
max(x)
min(x)
ans = 1500
ans = 60
ans = 34
This is an intuitive approach and works nicely for 2D arrays, without "randperm":
N = 36000; % for three hundred years
days = 30; % days
elem = 1500; % elements in ten years
min_x = 0; % daily minimum
max_x = 60; % daily maximum
tic
x = zeros(days, N);
for hh = 1:elem
% Add new candidates
inds = randi(days, N, 1);
inds = ((1:N).' - 1) * days + inds;
x(inds) = x(inds) + 1;
% Check
inds_chck = x > max_x;
any_inds_chck = any(inds_chck);
find_any_inds_chck = find(any_inds_chck);
ctrl = numel(find_any_inds_chck);
while ctrl>0
% First remove baddies
inds = inds(find_any_inds_chck);
x(inds) = x(inds) - 1;
% Then reassign to new candidates
inds = randi(days, ctrl, 1);
inds = (find_any_inds_chck.' - 1) * days + inds;
x(inds) = x(inds) + 1;
% Check again
inds_chck = x(:, find_any_inds_chck) > max_x;
any_inds_chck = any(inds_chck);
find_any_inds_chck = find(any_inds_chck);
ctrl = numel(find_any_inds_chck);
end
end
toc
But the price is a weird probability function:
hist(x(:), max_x - min_x + 1)
Note that the constraint has an obvious effect on the degrees of freedom as well.
Also note that they have tried to answer a similar question in Generate a random number with max, min and mean (average) in Matlab .