while loop in my program does not fill the second row - matlab

I have 2 inner while loops. Code writes the results to first row on the matrix but when it gets to second row, it just passes the other rows and doesn't fill the columns of the rows. How to solve it? The code and output are like below:
while i <= m-1
i
while a <= m-1
a
den1 = sqrt(((xy{i,j}-xy{a+1,b})^2+(xy{i,j+1}-xy{a+1,b+1})^2 ));
dMat(i,a) = den1;
a = a+1;
end
i = i+1;
end
i = 1
a = 1
a = 2
a = 3
a = 4
a = 5
a = 6
a = 7
a = 8
a = 9
a = 10
a = 11
a = 12
a = 13
a = 14
a = 15
a = 16
a = 17
a = 18
a = 19
a = 20
a = 21
a = 22
a = 23
a = 24
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
i = 11
i = 12
i = 13
i = 14
i = 15
i = 16
i = 17
i = 18
i = 19
i = 20
i = 21
i = 22
i = 23
i = 24

You have to restart a to a=1 for each iteration. Put it above the while a <= m-1

Related

Issue trying to sub sample an image

I am trying to resize a given image by sub-sampling it. I am using grayscae iamges.
The way I understand sub-sampling is basically this:
Let's say we have an 5x5 image and we want to resize it by a factor of 2, so the output image will be 3x3.
So if the 5x5 is: (1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20; 21 22 23 24 25)
Then the 3x3 will be: (1 3 5; 11 13 15; 21 23 25)
So if the factor is 2, for the first row we take the first sample and then every second sample and so on. The same for columns.
The code that I wrote for this is:
i = 0;
j = 0;
for row=1:old_rows
if mod((row-1), a) == 0
i = i + 1;
end
for col=1:old_cols
if mod((row-1), a) == 0 && mod((col-1), a) == 0
j = j + 1;
sub_sampled_I(i, j) = I(row, col); % I is the input image
end
end
j = 0;
end
sub_sampled_I = im2uint8(sub_sampled_I);
end
The issue is that the final image has nothing to do with the original. It is just a white image with some black points here and there. What I understand wrong about sub-sampling?

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.';

Octave: How can I index single element of an array?

I have the following code written in Octave:
1 %-----------------------------------------------------------------------------%
2 % INPUT SIGNAL %
3 %-----------------------------------------------------------------------------%
4
5 sampling_frequency = 8000;
6 sampling_period = 1/sampling_frequency;
7 samples_count = 8;
8 time_samples = sampling_period * (0 : samples_count-1);
9
10
11 amplitude_1 = 1;
12 frequency_1 = 1000;
13 phase_shift_1 = 0;
14 signal_samples_1 = amplitude_1*sin(2*pi * frequency_1 * ...
15 time_samples + phase_shift_1);
16
17
18 amplitude_2 = 0.5;
19 frequency_2 = 2000;
20 phase_shift_2 = 3*pi/4;
21 signal_samples_2 = amplitude_2*sin(2*pi * frequency_2 * ...
22 time_samples + phase_shift_2);
23
24 signal_samples = signal_samples_1 + signal_samples_2;
25 for samples_index = 0 : samples_count-1
26 fprintf(stdout, "Sample %d: ", samples_index)
27 endfor
28
As you may see, at line 25, I would like to print value of each sample at separate line, i.e.
Sample <sample_number>: <sample_value>
My question is: how can I index separate member of an array (e.g. input_signal array)?
I have found the solution by myself, so I would like to share it with you.
signal_samples = signal_samples_1 + signal_samples_2;
for samples_index = 1 : samples_count
fprintf(stdout, "Sample %d: %8.5f\n", samples_index-1, ...
signal_samples(samples_index:samples_index))
endfor
Output:
Sample 0: 0.35355
Sample 1: 0.35355
Sample 2: 0.64645
Sample 3: 1.06066
Sample 4: 0.35355
Sample 5: -1.06066
Sample 6: -1.35355
Sample 7: -0.35355

How do I get microsoft word or pycharm to read dot files as a graphic instead of code

When I open my .dot file with microsoft word or pycharm it shows me the code and that the code is error free but doesnt display the intended graphic.
I have extensively searched on google for a solution but google insists that microsoft word comes with the ability to read .dot files (which it seems to be able to read the code but not display the graphic). I downloaded the pycharm .dot reading plug-in and now pycharm can read the code and it says that the code is error free, but again it does not display the graphic.
I am unsure what code is necessary so I am just giving the whole thing.
digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname=helvetica] ;
edge [fontname=helvetica] ;
0 [label="failures <= 0.5\nsamples = 100.0%\nvalue = [0.502, 0.498]\nclass = fail", fillcolor="#e5813902"] ;
1 [label="higher_no <= 0.5\nsamples = 84.8%\nvalue = [0.422, 0.578]\nclass = pass", fillcolor="#399de545"] ;
0 -> 1 [labeldistance=2.5, labelangle=45, headlabel="True"] ;
2 [label="school_GP <= 0.5\nsamples = 79.2%\nvalue = [0.386, 0.614]\nclass = pass", fillcolor="#399de55e"] ;
1 -> 2 ;
3 [label="absences <= 1.5\nsamples = 23.0%\nvalue = [0.574, 0.426]\nclass = fail", fillcolor="#e5813942"] ;
2 -> 3 ;
4 [label="freetime <= 4.5\nsamples = 12.4%\nvalue = [0.419, 0.581]\nclass = pass", fillcolor="#399de547"] ;
3 -> 4 ;
5 [label="samples = 11.6%\nvalue = [0.379, 0.621]\nclass = pass", fillcolor="#399de563"] ;
4 -> 5 ;
6 [label="samples = 0.8%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
4 -> 6 ;
7 [label="health <= 1.5\nsamples = 10.6%\nvalue = [0.755, 0.245]\nclass = fail", fillcolor="#e58139ac"] ;
3 -> 7 ;
8 [label="samples = 2.2%\nvalue = [0.455, 0.545]\nclass = pass", fillcolor="#399de52a"] ;
7 -> 8 ;
9 [label="samples = 8.4%\nvalue = [0.833, 0.167]\nclass = fail", fillcolor="#e58139cc"] ;
7 -> 9 ;
10 [label="Walc <= 3.5\nsamples = 56.2%\nvalue = [0.31, 0.69]\nclass = pass", fillcolor="#399de58d"] ;
2 -> 10 ;
11 [label="Medu <= 3.5\nsamples = 46.4%\nvalue = [0.263, 0.737]\nclass = pass", fillcolor="#399de5a4"] ;
10 -> 11 ;
12 [label="samples = 29.4%\nvalue = [0.333, 0.667]\nclass = pass", fillcolor="#399de57f"] ;
11 -> 12 ;
13 [label="samples = 17.0%\nvalue = [0.141, 0.859]\nclass = pass", fillcolor="#399de5d5"] ;
11 -> 13 ;
14 [label="sex_M <= 0.5\nsamples = 9.8%\nvalue = [0.531, 0.469]\nclass = fail", fillcolor="#e581391d"] ;
10 -> 14 ;
15 [label="samples = 3.0%\nvalue = [0.267, 0.733]\nclass = pass", fillcolor="#399de5a2"] ;
14 -> 15 ;
16 [label="samples = 6.8%\nvalue = [0.647, 0.353]\nclass = fail", fillcolor="#e5813974"] ;
14 -> 16 ;
17 [label="reason_course <= 0.5\nsamples = 5.6%\nvalue = [0.929, 0.071]\nclass = fail", fillcolor="#e58139eb"] ;
1 -> 17 ;
18 [label="health <= 3.5\nsamples = 2.6%\nvalue = [0.846, 0.154]\nclass = fail", fillcolor="#e58139d1"] ;
17 -> 18 ;
19 [label="samples = 1.4%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
18 -> 19 ;
20 [label="reason_home <= 0.5\nsamples = 1.2%\nvalue = [0.667, 0.333]\nclass = fail", fillcolor="#e581397f"] ;
18 -> 20 ;
21 [label="samples = 0.6%\nvalue = [0.333, 0.667]\nclass = pass", fillcolor="#399de57f"] ;
20 -> 21 ;
22 [label="samples = 0.6%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
20 -> 22 ;
23 [label="samples = 3.0%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
17 -> 23 ;
24 [label="Fjob_teacher <= 0.5\nsamples = 15.2%\nvalue = [0.947, 0.053]\nclass = fail", fillcolor="#e58139f1"] ;
0 -> 24 [labeldistance=2.5, labelangle=-45, headlabel="False"] ;
25 [label="Fjob_health <= 0.5\nsamples = 15.0%\nvalue = [0.96, 0.04]\nclass = fail", fillcolor="#e58139f4"] ;
24 -> 25 ;
26 [label="freetime <= 1.5\nsamples = 14.8%\nvalue = [0.973, 0.027]\nclass = fail", fillcolor="#e58139f8"] ;
25 -> 26 ;
27 [label="Mjob_at_home <= 0.5\nsamples = 0.6%\nvalue = [0.667, 0.333]\nclass = fail", fillcolor="#e581397f"] ;
26 -> 27 ;
28 [label="samples = 0.2%\nvalue = [0.0, 1.0]\nclass = pass", fillcolor="#399de5ff"] ;
27 -> 28 ;
29 [label="samples = 0.4%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
27 -> 29 ;
30 [label="age <= 16.5\nsamples = 14.2%\nvalue = [0.986, 0.014]\nclass = fail", fillcolor="#e58139fb"] ;
26 -> 30 ;
31 [label="samples = 2.4%\nvalue = [0.917, 0.083]\nclass = fail", fillcolor="#e58139e8"] ;
30 -> 31 ;
32 [label="samples = 11.8%\nvalue = [1.0, 0.0]\nclass = fail", fillcolor="#e58139ff"] ;
30 -> 32 ;
33 [label="samples = 0.2%\nvalue = [0.0, 1.0]\nclass = pass", fillcolor="#399de5ff"] ;
25 -> 33 ;
34 [label="samples = 0.2%\nvalue = [0.0, 1.0]\nclass = pass", fillcolor="#399de5ff"] ;
24 -> 34 ;
}
I expect the above code to display a color-coded decision tree. Instead microsoft word, pycharm, and jupyter notebook are all returning the code.
A GraphViz .dot file is not an image file. It rather contains a logical description of some content (nodes and edges), along with some graphical clues how to display them (e.g. color, font sizes). The exact positions of each element are not defined in the .dot file at all. Calculating these positions, and rendering actual image files, is left to a layout engine, such as dot, or derived Javascript libraries (e.g. viz.js).
Almost no application can display GraphViz .dot files directly.
The confusion probably comes from a conflicting naming convention:
GraphViz .dot file (what we are talking about here)
Microsoft .dot file (what Word is expecting and Google might refer to)
A Microsoft .dot file is a template for a Microsoft Word (.doc) file.
In order to present your desired graphic, you need to run your .dot file through a layout engine to produce e.g. an .svg or .png file.
You can do this manually on your local PC or online (e.g. https://dreampuf.github.io/GraphvizOnline/ ), then save the resulting image.
You can present both .svg and .png images in Word (Insert / Image).
I have been able to get the .dot file to open properly using GVEdit (graphviz text editor). Here is a link to a post that answers my question. Graphviz: How to go from .dot to a graph?
Don't try to open dot file directly from word as it will give an error. Open Ms-word first then locate the dot file which you want to open.

MATLAB initializing variables in next for loop

x=[3 1 1 -5 -2 0 1 2 -2 2];
A=[4 2 6; 0 1 -3; -2 5 -2];
B=[-2 3 2; 1 5 5; -3 1 0];
sum=0;
for i=2:3
sum_j=0;
for j=1:2
sum_j=sum_j+A(1,j)*B(j,i);
end
sum=sum+A(2,i)*sum_j;
end
fprintf('(c) %g\n',sum);
>> (c) -32
-32 is a correct answer. However, if I initialize sum_j=0 outside of the the loop, it returns a different value.
sum=0;
sum_j=0;
for i=2:3
for j=1:2
sum_j=sum_j+A(1,j)*B(j,i);
end
sum=sum+A(2,i)*sum_j;
end
fprintf('(c) %g\n',sum);
>> (c) -98
Can anyone explain why this is happening?
In your first code sum_j gets reinitialized in the loop for i and in second code sum_j carry the value in each loop. Here is a simulation of your codes
first code:
sum = 0
i=2:
sum_j = 0
i=2,j=1
sum_j = 0 + 4*3 = 12
i=2,j=2
sum_j = 12 + 2*5 = 22
sum = 0 + 1*22 = 22
i=3
sum_j = 0
i=3,j=1
sum_j = 0 + 4*2 = 8
i=3,j=2
sum_j = 8 + 2*5 = 18
sum = 22 + -3*18 = -32
Second code
sum = 0
sum_j = 0
i=2:
i=2,j=1
sum_j = 0 + 4*3 = 12
i=2,j=2
sum_j = 12 + 2*5 = 22
sum = 0 + 1*22 = 22
i=3
i=3,j=1
sum_j = 22 + 4*2 = 30
i=3,j=2
sum_j = 30 + 2*5 = 40
sum = 22 + -3*40 = -98