Matlab table - Searching and isolating values in a table - matlab

I am trying to isolate data in a table based on user input.
I have the below-
sex=input("please input the gender (M/F): ", 's');
sysbp= input("enter styloic blood pressure: ");
diabp= input("enter dystolic blood pressure: ");
systoliclow=[-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;-inf;
120;120;120;120;120;120;120;120;120;120;
130;130;130;130;130;130;130;130;130;130;
140;140;140;140;140;140;140;140;140;140;
160;160;160;160;160;160;160;160;160;160]
systolichigh =[119;119;119;119;119;119;119;119;119;119;
129;129;129;129;129;129;129;129;129;129;
139;139;139;139;139;139;139;139;139;139;
159;159;159;159;159;159;159;159;159;159;
inf;inf;inf;inf;inf;inf;inf;inf;inf;inf]
diastoliclow=[-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90; 100; 100 ;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ;
-inf; -inf ; 80; 80 ; 85; 85 ; 90; 90;100;100 ]
diastolichigh=[79; 79 ;84; 84; 89; 89; 99; 99; inf; inf;
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf;
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf
79; 79 ;84; 84; 89; 89; 99; 99; inf; inf]
gender={'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M' ;
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M';
'F' ;'M'; 'F' ;'M'; 'F' ;'M'; 'F' ;'M';'F' ;'M' }
values= [-3 ;0; 0 ;0; 0; 1; 2 ;2; 3 ;3;
0;0;0;0;0;1;2;2;3;3;
0;1;0;1;0;1;2;2;3;3;
2;2;2;2;2;2;2;2;3;3;
3;3;3;3;3;3;3;3;3;3]
bpt= table(systoliclow, systolichigh, diastoliclow,diastolichigh, gender, values)
if sysbp>=bpt.systoliclow && sysbp<=bpt.systolichigh && diabp<=bpt.diastoliclow && diap>=bpt.diastolichigh && ismember(sex{'F','M'});
test = bpt.values
How do I loop through the table to get the values based on the user input.

You can use logical indexing, no loops required:
isIsolated = sysbp>=bpt.systoliclow & sysbp<=bpt.systolichigh & diabp>=bpt.diastoliclow & diabp<=bpt.diastolichigh & strcmp(bpt.gender,sex);
isolatedTable = bpt(isIsolated,:);
For inputs M, 120 and 85:
>> isolatedTable
isolatedTable =
1×6 table
systoliclow systolichigh diastoliclow diastolichigh gender values
___________ ____________ ____________ _____________ ______ ______
120 129 85 89 'M' 1
If you are only interested in the values variable:
>> isolatedValues = bpt(isIsolated,end)
isolatedValues =
2×1 table
values
______
1

You can query the rows which fit the user input and iterate through their ids:
rows = sysbp>=bpt.systoliclow & sysbp<=bpt.systolichigh & diabp>=bpt.diastoliclow & diabp<=bpt.diastolichigh & strcmp(sex, bpt.gender);
ind = find(rows == 1);
for i=ind
bpt(i, :)
end
The result:
ans =
systoliclow systolichigh diastoliclow diastolichigh gender values
___________ ____________ ____________ _____________ ______ ______
-Inf 119 90 99 'F' 2
-Inf 119 90 99 'M' 2

Related

My matlab code give me bad results - using the MARCOS algorithm

I followed all the steps of the MARCOS (Measurement of alternatives and ranking according to Compromise solution) algorithm. I tried it and I follow the algorithm but always the code gave me the same results.
Everything is okay, the problem is FKm and FKp, always give the same values.
For example, let's try the alternatives are:
Networks:
8 50 4 134 12 67
66 74 1 50 42 75
42 60 49 285 5 68
47 60 45 68 6 63
8 50 4 145 17 61
61 83 1 44 11 63
48 60 59 288 4 75
45 60 21 95 4 33
and the weights:
weights= 0.0360 0.1240 0.1040 0.3250 0.3070 0.1020
Code:
function Score = MARCOS(Networks,Weights)
global CostperByte;
global Security;
global DataRate;
global PacketDelay;
global PacketJitter;
global PacketLoss;
%NumberOfNetworks;
CostperByte=1;
Security=2;
DataRate=3;
PacketDelay=4;
PacketJitter=5;
PacketLoss=6;
SIZE = size(Networks);
NumberOfNetworks = SIZE(1);
NumberOfCriteria = SIZE(2);
AAI=zeros(1,NumberOfCriteria);
AI= zeros(1,NumberOfCriteria);
for j=1:NumberOfCriteria
if ( (j==DataRate) || (j==Security) ) % benifit
AAI(1,j)= min (Networks(:,j));
AI(1,j)=max (Networks(:,j));
else
AAI(1,j)=max (Networks(:,j));
AI(1,j)=min(Networks(:,j));
end
end
Networks= [ AAI ; Networks ; AI];
SIZE = size(Networks);
NumberOfNetworks = SIZE(1);
NumberOfCriteria = SIZE(2);
Normalization=[];
for i=1:NumberOfNetworks
for j=1:NumberOfCriteria
if ( (j==DataRate) || (j==Security) ) %%%%%%% data rate, security, bandwidth
Normalization(i,j) = ( Networks (i,j)) / AI(1,j) ;
else %%%%%%% jitter, delay,st,
Normalization(i,j) = AI(1,j) / ( Networks (i,j));
end
end
end
for i=1: NumberOfNetworks
for j=1: NumberOfCriteria
W(i,j)=Normalization(i,j)*Weights(j);
end
end
Saai= sum(W(1,:));
Sai= sum(W(NumberOfNetworks,:)) ;
W=W(2:NumberOfNetworks-1,:);
SIZE = size(W );
NumberOfNetworks = SIZE(1);
NumberOfCriteria = SIZE(2);
for i=1:NumberOfNetworks
S(i) =sum( W(i,:));
end
for i = 1: NumberOfNetworks
Km(i) = S(i)/ Saai;
Kp(i) = S(i) / Sai;
end
for i=1:NumberOfNetworks
FKm(i) = Kp(i)/( Kp(i)+Km(i));
FKm(i)
FKp(i) = Km(i)/( Kp(i)+Km(i));
end
for i=1:NumberOfNetworks
FK(i)= ( FKm(i) + (FKp(i)) ) / ( 1+ ( (1-FKp (i))/FKp(i) ) + ( (1-FKm(i))/FKm(i) ) ) ;
end
%%summation%%
Results=[];
for i=1:NumberOfNetworks
Results= [Results; FK(i) ];
end
Score= Results;
end```
**Tried the code above**

Data arrangement in vector

I have the following vector:
Here is the code to produce this vector:
A = [11 115 167 44 51 5 6];
B = [100 1 1 87];
C = [2000 625];
D = [81 623 45 48 6 14 429 456 94];
E = [89];
F = [44 846 998 2035 498 4 68 4 1 89];
G = {A,B,C,D,E,F};
[max_val, idx] = max(cellfun(#numel, G)); % Find max sizes of vectors
% Create vector with zeros filling open matrix space
LeftIndented = zeros(idx,max_val);
for k = 1:numel(G), LeftIndented(k,1:numel(G{k})) = G{k}; end
I would like to have a vector with:
Data to the right (zeros to the left)
Centered data (surrounded with zeros)
(Notice that if data cannot be exactly centered, it is ok if it is off by one vector space to the left)
How can I achieve this?
You can pad each vector with zeros:
A = [11 115 167 44 51 5 6];
B = [100 1 1 87];
C = [2000 625];
D = [81 623 45 48 6 14 429 456 94];
E = [89];
F = [44 846 998 2035 498 4 68 4 1 89];
G = {A,B,C,D,E,F};
[max_val, idx] = max(cellfun(#numel, G)); % Find max sizes of vectors
% Create vector with zeros filling open matrix space
LeftIndented = zeros(idx,max_val);
Centered = zeros(idx,max_val);
RightAligned = zeros(idx,max_val);
for k = 1:numel(G)
LeftIndented(k,1:numel(G{k})) = G{k};
l = length(G{k});
padding = max_val - l;
leftPadding = floor(padding / 2);
Centered(k, :) = [zeros(1, leftPadding), G{k}, zeros(1, padding - leftPadding)];
RightAligned(k, :) = [zeros(1, padding), G{k}];
end
This is equivalent to
A = [11 115 167 44 51 5 6];
B = [100 1 1 87];
C = [2000 625];
D = [81 623 45 48 6 14 429 456 94];
E = [89];
F = [44 846 998 2035 498 4 68 4 1 89];
G = {A,B,C,D,E,F};
[max_val, idx] = max(cellfun(#numel, G)); % Find max sizes of vectors
% Create vector with zeros filling open matrix space
LeftIndented = zeros(idx,max_val);
Centered = zeros(idx,max_val);
RightAligned = zeros(idx,max_val);
for k = 1:numel(G)
LeftIndented(k,1:numel(G{k})) = G{k};
l = length(G{k});
padding = max_val - l;
leftPadding = floor(padding / 2);
Centered(k, 1 + leftPadding:leftPadding + l) = G{k};
RightAligned(k, 1 + padding:end) = G{k};
end
but in the second code the values of the vectors are written into the correct position in a zero vector.
A solution using strjust:
A = [11 115 167 44 51 5 6];
B = [100 1 1 87];
C = [2000 625];
D = [81 623 45 48 6 14 429 456 94];
E = [89];
F = [44 846 998 2035 498 4 68 4 1 89];
G = {A,B,C,D,E,F};
data = [G{:}];
N = cellfun(#numel, G);
M = max(N);
idx = char((N.' >= (1:M))+32);
Le = strjust(idx, 'left');
Ri = strjust(idx, 'right');
Ce = strjust(idx, 'center');
LeftAdjusted = zeros(M, N);
RightAdjusted = zeros(M, N);
Centered = zeros(M, N);
LeftAdjusted(Le.' ~= ' ') = data;
RightAdjusted(Ri.' ~= ' ') = data;
Centered(Ce.' ~= ' ') = data;
LeftAdjusted = LeftAdjusted.';
RightAdjusted = RightAdjusted.';
Centered = Centered.';

2-DCT Image compression matlab

Problem:
I tried implementing Discrete Cosine Transformation compression using matlab.
Input image would a jpg image (Lena) having a size 512 X 512.
There are two stages namely compression and decompression.
Compression and Quantization:
The input image is converted to YCbCr component. Then Y component is taken up
for compression. Further DCT will quantized.
Quantization and Decompression:
The quantized image is undergoes dequantization for decompression.
Issues:
Rectangular boxes are spotted in the decompressed version of the image. Is anything wrong with the code?
For your inference, below are the sample input and output images and followed by the matlab code.
Input image:
Y Component in YCbCr:
Output image:
Code:
clc;
clear all;
close all;
I = imread('lena512.jpg');
figure, imshow(I);
% Y = I;
YCbCr = rgb2ycbcr(I);
figure, imshow(YCbCr);
Y = YCbCr(:,:, 1);
figure, imshow(Y);
[h, w] = size(Y);
r = h/8;
c = w/8;
s = 1;
q50 = [16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
% COMPRESSION
for i=1:r
e = 1;
for j=1:c
block = Y(s:s+7,e:e+7);
cent = double(block) - 128;
for m=1:8
for n=1:8
if m == 1
u = 1/sqrt(8);
else
u = sqrt(2/8);
end
if n == 1
v = 1/sqrt(8);
else
v = sqrt(2/8);
end
comp = 0;
for x=1:8
for y=1:8
comp = comp + cent(x, y)*(cos((((2*(x-1))+1)*(m-1)*pi)/16))*(cos((((2*(y-1))+1)*(n-1)*pi)/16));
end
end
F(m, n) = v*u*comp;
end
end
for x=1:8
for y=1:8
cq(x, y) = round(F(x, y)/q50(x, y));
end
end
Q(s:s+7,e:e+7) = cq;
e = e + 8;
end
s = s + 8;
end
% % % % % % % % % % % % % % %
% % DECOMPRESSION
% % % % % % %
s = 1;
for i=1:r
e = 1;
for j=1:c
cq = Q(s:s+7,e:e+7);
for x=1:8
for y=1:8
DQ(x, y) = q50(x, y)*cq(x, y);
end
end
for m=1:8
for n=1:8
if m == 1
u = 1/sqrt(8);
else
u = sqrt(2/8);
end
if n == 1
v = 1/sqrt(8);
else
v = sqrt(2/8);
end
comp = 0;
for x=1:8
for y=1:8
comp = comp + u*v*DQ(x, y)*(cos((((2*(x-1))+1)*(m-1)*pi)/16))*(cos((((2*(y-1))+1)*(n-1)*pi)/16));
end
end
bf(m, n) = round(comp)+128;
end
end
Org(s:s+7,e:e+7) = bf;
e = e + 8;
end
s = s + 8;
end
imwrite(Y, 'F:\workouts\phd\jpeg\input.jpg');
imwrite(uint8(Org), 'F:\workouts\phd\jpeg\output.jpg');
return;
Can you suggest me where the error is? It would be helpful.

JPEG compression in matlab

I have made a JPEG compression using matlab, but i feel confused how to add zig zag scanning, rle, and huffman. I hope you can help me.
This is the code that i have made. Input file is an image (*.bmp).
% dctmtx generates an 8 * 8
dct_matrix = dctmtx(8);
% define function handlers for DCT
dct = #(block_struct) dct_matrix * block_struct.data * dct_matrix';
% define function handlers for inverse DCT
idct = #(block_struct) dct_matrix' * block_struct.data * dct_matrix;
% quantization tables
q_max = 255;
% the quality factor is hardwired here
qf = get(handles.popupmenu1,'Value');
% quantization table for luminance
q_y = ...
[16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99].*qf;
% quantization table for chrominance
q_c = ...
[17 18 24 47 99 99 99 99;
18 21 26 66 99 99 99 99;
24 26 56 99 99 99 99 99;
47 66 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99;
99 99 99 99 99 99 99 99].*qf;
% RGB to YCbCr
orig = handles.citra;
ycc = rgb2ycbcr(im2double(orig));
%display the ycbcr
y = ycc(:, :, 1);
cb = ycc(:, :, 2);
cr = ycc(:, :, 3);
% Discrete cosine transform, with scaling before quantization
% q_max is the quantization
% set up the blockprocess functions for the DCT
y = blockproc(y, [8 8], dct) .*q_max;
cb = blockproc(cb, [8 8], dct) .*q_max;
cr = blockproc(cr, [8 8], dct) .*q_max;
% implement the quantization
y = blockproc(y, [8 8], #(block_struct) round(round(block_struct.data) ./ q_y));
cb = blockproc(cb, [8 8], #(block_struct) round(round(block_struct.data) ./ q_c));
cr = blockproc(cr, [8 8], #(block_struct) round(round(block_struct.data) ./ q_c));
% Dequantize DCT coefficients
y = blockproc (y, [ 8 8 ], #(block_struct) block_struct.data .*q_y ) ;
cb = blockproc (cb, [ 8 8 ], #(block_struct) block_struct.data .*q_c ) ;
cr = blockproc (cr, [ 8 8 ], #(block_struct) block_struct.data .*q_c ) ;
% Inverse DCT
y = blockproc (y ./ q_max , [ 8 8 ] , idct);
cb = blockproc (cb ./ q_max , [ 8 8 ] , idct);
cr = blockproc (cr ./ q_max , [ 8 8 ] , idct);
% Concatenate the channels
ycc1 = ycbcr2rgb(cat(3, y, cb ,cr));
axes(handles.axes_hasil);
imshow(ycc1);
Please check "Digital image processing Using MATLAB" book, there is a detailed chapter of JPEG Compression
it has some helpful code for JPEG compression
for the rle, this code might of help
function out=rle(image)
L=prod(size(image));
im=reshape(image,1,L);
x=1;
out=[];
while L~=0;
temp=min(find(im==x));
if isempty(temp)
out=[out L];
break;
end;
out=[out temp-1];
x=1-x;
im=im(temp:L);
L=L-temp+1;
end;
end

How do I add lines connecting corresponding boxes, that I've drawn on a plot? (MATLAB)

I have a matrix (Data) which looks like this:
(start) (stop) (strand) (gene number)
[ 1 29 1 1]
[ 32 38 1 1]
[ 44 60 1 1]
[ 66 70 0 2]
[ 75 80 0 2]
[ 81 88 0 3]
[ 99 102 0 3]
[ 111 160 0 3]
[ 166 170 1 4]
[ 171 188 1 4]
which I have plotted onto a graph using the first two columns as X positions, and a set Y position. This is the code I have up till now:
if nargin<4, strands = 0; end;
if nargin<3, height = 0.1; end;
if nargin<2, y = 2.1; end;
for k=1:size(cds,1),
xc = [cds(k,1) cds(k,2) cds(k,2) cds(k,1)];
if strands,
if cds(k,3), % minus strand
yc = [y y y-height/2 y-height/2];
c = 'r';
else % plus strand
yc = [y+height/2 y+height/2 y y];
c = 'b';
end
else
yc = [y+height/2 y+height/2 y-height/2 y-height/2];
c = 'b';
end
h(k) = patch(xc,yc,c);
end
What I'm trying to do is add lines underneath each 'box' which corresponds to the gene number (4th collumn of the data matrix). How would I go about doing this with the plot function?
It's not clear from your question how you want the lines to indicate the gene numbers, I assume you want different colors for each type. Here is how I would do it:
cds = [
1 29 1 1
32 38 1 1
44 60 1 1
66 70 0 2
75 80 0 2
81 88 0 3
99 102 0 3
111 160 0 3
166 170 1 4
171 188 1 4
];
strands = 0;
height = 0.1;
y = 2.1;
[g,gIdx,gNum] = unique(cds(:,4));
clr = 'gcmykrb';
for k=1:size(cds,1),
xc = [cds(k,1) cds(k,2) cds(k,2) cds(k,1)];
if strands,
if cds(k,3), % minus strand
yc = [y y y-height/2 y-height/2];
c = 'r';
else % plus strand
yc = [y+height/2 y+height/2 y y];
c = 'b';
end
else
yc = [y+height/2 y+height/2 y-height/2 y-height/2];
c = 'b';
end
h(k) = patch(xc,yc,c);
hLine(k) = line([cds(k,1) cds(k,2)], [y-3*height/4 y-3*height/4], ...
'LineWidth',5, 'Color',clr(gNum(k)));
end
legend(hLine(gIdx), num2str(g), 'Orientation','horizontal')