Syntax error for rectangular image - matlab

There is an image X of N*M size row M,column N. There are other 2 images A,B of same size as X. The objective is to shuffle the rows of X with the rows extracted from A and shuffle the columns of X with the columns extracted from B resulting in a totally modified img.
I am stuck at the point where simultaneously this is occuring. I am aware about the colon operator with which the code runs but for a square image. Please help how to go about it.
X=imread('picture.jpg');
[r c]=size(X);
[dummy,rowscrambleIdx]=sort(A,1);
X_shuffled=X;
[dummy,colscrambleIdx]=sort(B,2);
EDIT: The following code works for square image and I want to do similar operation for a rectangular sized image. However, this code does not work for rectangular sized image. I have tried to make the first code follow a similar logic but it does not work for a non-square RGB image having say 256*240*3 size
X=imread('picture.jpg');
[dummy,scrambleIdx]=sort(A(:));
X_shuffled=X;
X_shuffled(:)=A(scrambleIdx);
%now unscrambling
[dummy,unscrambleIdx] = sort(scrambleIdx);
X_recovered=X;
X_recovered(:)=X_shuffled(unscrambleIdx);

Why not use randi to randomly create some number of indicies to pull from A, and some to pull from B?
Example:
m=10;
n=5;
A=rand(m,n)
B=ones(m,n)
%3x1 vector of random ints b/w 1 and 10
index=randi([1 10],3,1);
rand_row = A(index,:);
B(index,:)=rand_row
Then output is:
index =
10
9
4
A =
0.9797 0.1174 0.7303 0.6241 0.2619
0.4389 0.2967 0.4886 0.6791 0.3354
0.1111 0.3188 0.5785 0.3955 0.6797
0.2581 0.4242 0.2373 0.3674 0.1366
0.4087 0.5079 0.4588 0.9880 0.7212
0.5949 0.0855 0.9631 0.0377 0.1068
0.2622 0.2625 0.5468 0.8852 0.6538
0.6028 0.8010 0.5211 0.9133 0.4942
0.7112 0.0292 0.2316 0.7962 0.7791
0.2217 0.9289 0.4889 0.0987 0.7150
B =
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
B =
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
0.2581 0.4242 0.2373 0.3674 0.1366
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000
0.7112 0.0292 0.2316 0.7962 0.7791
0.2217 0.9289 0.4889 0.0987 0.7150

Related

How to test a correlation?

How to test wheater there is a correlation between two parameters? I have n-dimensional parameters a and b in matlab? I would like to find the correlation parametr is there a simply function for it? Thank you
I tried:
C = [a b];
cov(C)
I got
0.0001 0.0029
0.0029 1.0668
And
corrcoef(C)
with the result
1.0000 0.3682
0.3682 1.0000
I sould look at diagonnal members - they are 1, so it is correlation?
Is that right?
1
1.0000 -0.3143
-0.3143 1.0000
2
1.0000 0.4248
0.4248 1.0000
3
1.0000 -0.3290
-0.3290 1.0000
4
1.0000 -0.1397
-0.1397 1.0000
5
1.0000 -0.1506
-0.1506 1.0000
6
1.0000 0.3682
0.3682 1.0000

Matlab find minimum value by ignoring zero and certain condition

so i have this data
yi =
-1
1
1
-2
4
data=
1.0000 0 1.0000 0.2000 1.0000 1.0000
0 0 0 0.5000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0 0 0 0 1.0000 0
1.0000 0 0 0 1.0000 0
and i short that data become like this
sdata =
0 0.2000 1.0000 1.0000 1.0000 1.0000
0 0 0 0.5000 1.0000 1.0000
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000
0 0 0 0 0 1.0000
0 0 0 0 1.0000 1.0000
with this rule
if yi >= 0 then output = max row sdata
if yi < 0 theris 2 conditions:
a. if sdata member only 0 and 1 then output = min row sdata =0
b. if sdata member including number between 0 and 1 then output = min sdata except zero
so i want to find an output like this
output=
0.2000
1.0000
1.0000
1.0000
0
1.0000
i still stuck to this point
for i=1:5
if yi(i)>=0
output(i)=max(sdata(i,:));
else
output(i)=min(sdata(i,:));
end;
end;
outputnya=output'
Try this:
else
minVal = min(sdata(i,sdata(i,:)>0))
if minVal == 1
output(i)=min(sdata(i,:));
else
output(i) = minVal;
end;
end;
Explanation: sdata(i,:)>0 returns table of 0 and 1, sdata(i,sdata(i,:)>0) returns element which meet specified condition.

How to find non zero minimum array in a 2D matrix in matlab

I have a 2D matrix as shown below:
A =
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8193 0.6429 0.4731
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8172 0.6395 0.4696
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8099 0.6285 0.4582
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7938 0.6066 0.4364
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7588 0.5675 0.4006
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8527 0.6739 0.5039 0.3477
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8825 0.7371 0.5803 0.4264 0.2807
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.7928 0.6327 0.4838 0.3406 0.2054
1.0000 1.0000 1.0000 1.0000 1.0000 1.0000 0.8846 0.7945 0.6560 0.5173 0.3816 0.2469 0.1131
1.0000 1.0000 1.0000 1.0000 1.0000 0.8598 0.7438 0.6373 0.5194 0.3988 0.2785 0.1522 0
1.0000 1.0000 1.0000 0.8710 0.7956 0.6954 0.5935 0.4916 0.3856 0.2799 0.1814 0.0834 0
0.7915 0.7845 0.7581 0.6884 0.6159 0.5326 0.4432 0.3500 0.2514 0.1538 0.0838 0 0
0.5972 0.5882 0.5596 0.5085 0.4471 0.3760 0.2967 0.2137 0.1163 0 0 0 0
0.4208 0.4116 0.3836 0.3389 0.2881 0.2274 0.1540 0.0919 0 0 0 0 0
0.2629 0.2536 0.2244 0.1755 0.1388 0.0915 0 0 0 0 0 0 0
0.1235 0.1155 0.0850 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
I want to remove its zero arrays and then find the minimum value of each column and put those minimum values in a vector.
I just used find(A ~= 0) but it's not working.
How can I do that?
Replace all of the zeroes with NaN, then apply min to each column independently. Setting values to NaN in a matrix / vector allows min to ignore those values, so you'd effectively be ignoring the zeroes. Assuming that your matrix is in A, do this:
Anan = A;
Anan(A == 0) = NaN;
out = min(Anan, [], 1);

MatLab: Create 3D Histogram from sampled data

I have sampled data in the interval [0,1] in an Array transitions=zeros(101,101) which I want to plot as a 3D-histogram. transitions is filled with data similar to the example data provided at the end of this thread.
The first columns refers to the first observed variable X, the second column to the second variable Y and the third column is the normalized frequency. I.e. for the first row: the observed normalized frequency of the variable pair (0,0) is 0.9459. The sum of the normalized frequencies for (0,Y)thus is 1.
I tried to make (sort of) a 3D histogram with the following code:
x_c = (transitions(:,1) * 100)+1;
y = (transitions(:,2) * 100)+1;
z = transitions(:,4);
%A = zeros(10,10);
A = zeros(max(x_c),max(y));
for i = 1:length(x_c)
try
if(z(i)>0)
A(int32(x_c(i)), int32(y(i))) = abs(log(z(i)));
else
% deal with exceptions regarding log(0)
A(int32(x_c(i)), int32(y(i))) = 0;
end
catch
disp('');
end
end
bar3(A);
However, since it is sampled data in a discrete space A the output looks like the plot below. This is somehow misleading as there are 'gaps' in the plot (z-value = 0 for coordinates where I have no sampled data). I rather would like to have the sampled data being assigned to their corresponding plots, thus resulting in a 'real' 3d histogram.
By the way, as a result of my 'hack' of creating A also the x-,y- and z-scale is not correct. The 3D histogram's axes (all three) should be in the interval of [0,1].
ans =
0 0 0.9459
0 0.0500 0.0256
0 0.1000 0.0098
0 0.1100 0.0004
0 0.1500 0.0055
0 0.1600 0.0002
0 0.2000 0.0034
0 0.2100 0.0001
0 0.2500 0.0024
0 0.2600 0.0001
0 0.3000 0.0018
0 0.3200 0.0000
0 0.3700 0.0000
0 0.4000 0.0010
0 0.4200 0.0000
0 0.4500 0.0007
0 0.5000 0.0007
0 0.5300 0.0000
0 0.5500 0.0005
0 0.6000 0.0005
0 0.6300 0.0000
0 0.7000 0.0002
0 0.7400 0
0 0.7500 0.0003
0 0.7900 0.0000
0 0.8000 0.0002
0 0.8400 0.0000
0 0.8500 0.0002
0 0.8900 0.0000
0 0.9000 0.0002
0 0.9500 0.0001
0 1.0000 0.0001
0.0500 0 0.0235
0.0500 0.0500 0.0086
0.0500 0.1000 0.0045
. . .
. . .
. . .
. . .
. . .
0.9500 0.9000 0.0035
0.9500 0.9500 0.0066
0.9500 1.0000 0.0180
1.0000 0 0.0001
1.0000 0.0500 0.0001
1.0000 0.1000 0.0001
1.0000 0.1100 0.0000
1.0000 0.1500 0.0001
1.0000 0.1600 0.0000
1.0000 0.2000 0.0001
1.0000 0.2100 0.0000
1.0000 0.2500 0.0001
1.0000 0.2600 0.0000
1.0000 0.3000 0.0001
1.0000 0.3200 0.0000
1.0000 0.3700 0.0000
1.0000 0.4000 0.0002
1.0000 0.4200 0
1.0000 0.4500 0.0002
1.0000 0.5000 0.0003
1.0000 0.5300 0.0000
1.0000 0.5500 0.0004
1.0000 0.6000 0.0004
1.0000 0.6300 0.0000
1.0000 0.7000 0.0007
1.0000 0.7400 0.0000
1.0000 0.7500 0.0010
1.0000 0.7900 0.0000
1.0000 0.8000 0.0015
1.0000 0.8400 0.0001
1.0000 0.8500 0.0024
1.0000 0.8900 0.0002
1.0000 0.9000 0.0042
1.0000 0.9500 0.0111
1.0000 1.0000 0.3998
I found a solution by working on the non-aggregated data. In particular each row of the data set transitions contains one observation of Xand Y. I used the code below to produce a normalized 3D histogram (and a 2D map) as folllows:
function createHistogram(transitions)
uniqueValues = unique(transitions(:,1));
biases = cell(numel(uniqueValues),1);
for i = 1:numel(uniqueValues)
start = min(find(transitions(:,1) == uniqueValues(i)));
stop = max(find(transitions(:,1) == uniqueValues(i)));
biases(i) = mat2cell(transitions(start:stop,2));
end
combinedBiases = padcat(biases{1},biases{2},biases{3},biases{4},...
biases{5},biases{6},biases{7},biases{8},biases{9},biases{10},...
biases{11},biases{12},biases{13},biases{14},biases{15},biases{16},...
biases{17},biases{18},biases{19});
bins = 0:0.1:1;
[f, x] = hist(combinedBiases, bins);
%
% normalize
%
for i = 1:numel(f(1,:))
for j = 1:numel(f(:,i))
f(j,i) = f(j,i)/numel(biases{i});
end
end
bHandle = bar3(x, f);
ylim([-0.04,1.04])
for k = 1:length(bHandle)
zdata = get(bHandle(k),'ZData');
set(bHandle(k),'CData',zdata, 'FaceColor','interp');
end
colormap('autumn');
hcol = colorbar();
axis('square');
cpos=get(hcol,'Position');
cpos(4)=cpos(4)/3; % Halve the thickness
cpos(2)=0.4; % Move it down outside the plot#
cpos(1)=0.82;
set(hcol, 'Position',cpos);
xlabel('Enrollment biases');
ylabel('Aging biases');
zlabel('Bias transition probability');
title(strcat('Probability mass function of bias transitions (', device,')'));
set(gca,'XTick',0:2:20);
set(gca,'XTickLabel',0:0.1:1);
print('-dpng','-r600',strcat('tau_PMF3D_enrollment-ageing-', device));
view(2);
cpos(1)=0.84;
set(hcol, 'Position',cpos);
print('-dpng','-r600',strcat('tau_PMF2D_enrollment-ageing-', device));
end
From the comment on the question it appears you have the values you want to represent each bin count. If so an alternative solution is to plot using hist3 with "junk" data using correct x and y scales and then update the zdata of the surface object created with your bin data (modified to be in the correct format).
This modification to the bin data is fairly simple and consists of reshaping into a matrix then replicating and padding all the elements, the method is included in the code below.
Based on the ans variable at the end of the question, assuming
ans(:,1) gives x values
ans(:,2) gives y values
ans(:,3) gives the normalised bin counts
code
%// Inputs
zdata=ans(:,3); %// zdata=rand(21*21,1); % for testing
xvalues = 0:0.05:1;
yvalues = 0:0.05:1;
%// plot with junk data, [0,0] in this case
nx = numel(xvalues); ny = numel(yvalues);
bincenters = { xvalues , yvalues };
hist3([0,0],bincenters);
Hsurface = get(gca,'children');
%// apply bin count format
pad = [0 0 0 0 0;0 1 1 0 0;0 1 1 0 0;0 0 0 0 0;0 0 0 0 0]; %// padding for each point
ztrans=kron(reshape(zdata,[nx,ny]),pad); %// apply padding to each point
%// update plot
set(Hsurface,'ZData',ztrans)
%// to set colour based on bar height
colormap('autumn');
set(Hsurface,'CData',ztrans,'FaceColor','interp')
output

Plot a disc in Matlab, color varying with angle

I would like to represent some data depending on the angle of measure. I have something like that:
angle=[0 90 180 270 360];
value=[1 2 3 4];
In my case, length(angle)=73
How can I plot a disc in which the color of the radius line varies according to the vector value.
I would like something like imagesc but in a disc.
I unsuccessfully tried using rose, but it is limited to a max of 20 angles.
This is another alternative that does not use 3D graphics, only 2D. It uses the primitive patch command that draws triangles. Here is a result:
function PlotDisc
thetas=(0:5:360)'*pi/180;
% r=(0:0.1:1)';
r = 1;
[x,y]=pol2cart(thetas,r);
colMap = GetColMap();
for i=1:numel(x)-1
X = [0 x(i) x(i+1) 0];
Y = [0 y(i) y(i+1) 0];
col = i / (numel(x)-1) * numel(colMap);
patch(X,Y,col,'EdgeAlpha',0);
end
axis equal
end
function colMap = GetColMap()
colMap = ...
[0.0417 0 0
0.0833 0 0
0.1250 0 0
0.1667 0 0
0.2083 0 0
0.2500 0 0
0.2917 0 0
0.3333 0 0
0.3750 0 0
0.4167 0 0
0.4583 0 0
0.5000 0 0
0.5417 0 0
0.5833 0 0
0.6250 0 0
0.6667 0 0
0.7083 0 0
0.7500 0 0
0.7917 0 0
0.8333 0 0
0.8750 0 0
0.9167 0 0
0.9583 0 0
1.0000 0 0
1.0000 0.0417 0
1.0000 0.0833 0
1.0000 0.1250 0
1.0000 0.1667 0
1.0000 0.2083 0
1.0000 0.2500 0
1.0000 0.2917 0
1.0000 0.3333 0
1.0000 0.3750 0
1.0000 0.4167 0
1.0000 0.4583 0
1.0000 0.5000 0
1.0000 0.5417 0
1.0000 0.5833 0
1.0000 0.6250 0
1.0000 0.6667 0
1.0000 0.7083 0
1.0000 0.7500 0
1.0000 0.7917 0
1.0000 0.8333 0
1.0000 0.8750 0
1.0000 0.9167 0
1.0000 0.9583 0
1.0000 1.0000 0
1.0000 1.0000 0.0625
1.0000 1.0000 0.1250
1.0000 1.0000 0.1875
1.0000 1.0000 0.2500
1.0000 1.0000 0.3125
1.0000 1.0000 0.3750
1.0000 1.0000 0.4375
1.0000 1.0000 0.5000
1.0000 1.0000 0.5625
1.0000 1.0000 0.6250
1.0000 1.0000 0.6875
1.0000 1.0000 0.7500
1.0000 1.0000 0.8125
1.0000 1.0000 0.8750
1.0000 1.0000 0.9375
1.0000 1.0000 1.0000] ;
end
Some time ago I had to plot polar data and looked for something similar. I do not know if it corresponds to your needs but I adapted my solution to your problem, here it is:
theta=(0:360)'*pi/180;
r=(0:0.1:1)';
value=repmat([360 (1:360)],size(r,1),1);
[THETA,R]=meshgrid(theta,r);
[X,Y]=pol2cart(THETA,R);
surf(X,Y,value,'edgecolor','none');
view(0,90);
UPDATE: You are absolutely right Andrey, and I suspect the 3D graphics to take more memory and thus display a lot slower but at the time I did not find any ohter way. Your solution is better, I will use it from now. The only thing I would change is the for loop:
r=1;
dtheta=0.01;
theta=linspace(0,2*pi,100)';
n=numel(theta);
[x,y]=pol2cart(theta,r);
figure;
cmap=colormap(jet);
X=[zeros(1,n-1);x(1:end-1)';x(2:end)';zeros(1,n-1)];
Y=[zeros(1,n-1);y(1:end-1)';y(2:end)';zeros(1,n-1)];
C=numel(cmap)*(1:n-1)/n-1;
patch(X,Y,C,'EdgeAlpha',0);
axis equal;