Line break other than \n in MATLAB - matlab

The following b variable seems to have a last character corresponding to a line break but it is not the classical '\n' character. This is very embarrassing for print using this variable:
K>> b
b =
toto_titi
K>> fprintf('This strange variable (%s) is very strange !!!\n', b);
This strange variable (toto_titi
) is very strange !!!
Indeed, the needed print would be:
This strange variable (toto_titi) is very strange !!!
Below, few observed facts with this variable:
K>> class(b)
ans =
char
K>> [b 'end']
ans =
toto_titi
end
K>> b(end)
ans =
K>> regexp(b,'_', 'split')
ans =
'toto' 'titi…'
I confess that I do not exactely understand for the three dots in 'titi…' but I suppose that here is a part of the explanation !!!
A quick and dirty method will be to remove the last invisible character that cause the line break but if in other application the last caharacter in the b variable is a "real" character we will lost this last character (ex. toto_tit in place of toto_titi).
A MATLAB equivalent of the python strip() exist ?
What is this invisible character in the b variable ?
EDIT 1:
A good idea, from rahnema1, is to determine what is the ASCII code for this invisible character:
K>> double(b)
ans =
116 111 116 111 95 116 105 116 105 13
K>> for i = 1 : 60
str = [num2str(i) ' ' char(i) ' '...
num2str(i+32) ' ' char(i+32) ' '...
num2str(i+64) ' ' char(i+64)];
disp(str)
end
1 33 ! 65 A
2 34 " 66 B
3 35 # 67 C
4 36 $ 68 D
5 37 % 69 E
6 38 & 70 F
7 39 ' 71 G
8 40 ( 72 H
9 41 ) 73 I
10
42 * 74 J
11 43 + 75 K
12 44 , 76 L
13
45 - 77 M
14 46 . 78 N
15 47 / 79 O
16 48 0 80 P
17 49 1 81 Q
18 50 2 82 R
19 51 3 83 S
20 52 4 84 T
21 53 5 85 U
22 54 6 86 V
23 55 7 87 W
24 56 8 88 X
25 57 9 89 Y
26 58 : 90 Z
27 59 ; 91 [
28 60 < 92 \
29 61 = 93 ]
30 62 > 94 ^
31 63 ? 95 _
32 64 # 96 `
33 ! 65 A 97 a
34 " 66 B 98 b
35 # 67 C 99 c
36 $ 68 D 100 d
37 % 69 E 101 e
38 & 70 F 102 f
39 ' 71 G 103 g
40 ( 72 H 104 h
41 ) 73 I 105 i
42 * 74 J 106 j
43 + 75 K 107 k
44 , 76 L 108 l
45 - 77 M 109 m
46 . 78 N 110 n
47 / 79 O 111 o
48 0 80 P 112 p
49 1 81 Q 113 q
50 2 82 R 114 r
51 3 83 S 115 s
52 4 84 T 116 t
53 5 85 U 117 u
54 6 86 V 118 v
55 7 87 W 119 w
56 8 88 X 120 x
57 9 89 Y 121 y
58 : 90 Z 122 z
59 ; 91 [ 123 {
60 < 92 \ 124 |
EDIT 2:
Works around the idea of rahnema1 and excaza:
. If we know the ASCII code (by using strrep):
K>> fprintf('This strange variable (%s) is very stange !!!\n', strrep(b,char(13),''));
This strange variable (toto_titi) is very stange !!!
. For remove leading and trailing whitespace (by using strtrim):
fprintf('This strange variable (%s) is very stange !!!\n', strtrim(b));
This strange variable (toto_titi) is very stange !!!

Related

How to sum columns of a matrix for a specified number of columns?

I have a matrix A of size 2500 x 500. I want to sum each 10 columns and get the result as a matrix B of size 2500 x 50. That is, the first column of B is the sum of the first 10 columns of A, the second column of B is the sum of second 10 columns of A, and so on.
How can I do that without a for loop? Since I have to do that hundreds of times and it is highly time consuming to do that using for loop.
First, we "block reshape" A, such that we have the desired number of columns. Therefore, we shamelessly steal the code from the great Divakar, and put in some minimal effort to generalize it. Then, we just need to sum along the second axis, and reshape to the original form.
Here's an example with five columns to be summed:
% Sample input data
A = reshape(1:100, 10, 10).'
[r, c] = size(A);
% Number of columns to be summed
n_cols = 5;
% Block reshape to n_cols, see https://stackoverflow.com/a/40508999/11089932
B = reshape(permute(reshape(A, r, n_cols, []), [1, 3, 2]), [], n_cols);
% Sum along second axis
B = sum(B, 2);
% Reshape to original form
B = reshape(B, r, c / n_cols)
That's the output:
A =
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 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
B =
15 40
65 90
115 140
165 190
215 240
265 290
315 340
365 390
415 440
465 490
Hope that helps!
This can be done with splitapply. An advantage of this approach is that it works even if the group size does not divide the number of columns (the last group is smaller):
A = reshape(1:120, 12, 10).'; % example 10×12 data (borrowed from HansHirse)
n_cols = 5; % number of columns to sum over
result = splitapply(#(x)sum(x,2), A, ceil((1:size(A,2))/n_cols));
In this example,
A =
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 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120
result =
15 40 23
75 100 47
135 160 71
195 220 95
255 280 119
315 340 143
375 400 167
435 460 191
495 520 215
555 580 239

How to check 4 neighbor connectivity in grayscale image

mat =
147 155 139 104 84 139
136 134 99 73 60 144
98 82 60 54 47 118
86 59 46 48 38 90
88 66 50 44 35 67
88 75 53 40 43 48
p = mat(3,3)
q = mat(2,5)
V = [1:60]
all i want is to check if there exist a path between pixel p and q using vector V, for example given matrix above there exist a paths: path1: (3,3) , (3,4) , (3,5) , (2,5) path2: (3,3) , (4,3) , (4,4) , (4,5) , (3,5) , (2,5) and many other paths, Note: pixel value in each path's coordinate should be in 'V' and obviously p and q should also be in 'V'. that's what i am trying to achieve in MATLAB
Code i've written so far:
mat =
147 155 139 104 84 139
136 134 99 73 60 144
98 82 60 54 47 118
86 59 46 48 38 90
88 66 50 44 35 67
88 75 53 40 43 48
p = mat(3,3)
q = mat(2,5)
V = [1:60]
% need to check N4 connectivity through pixel value p to q
cc = bwconncomp(mat,4)
for i=1:cc.NumObjects
pix = cc.PixelIdxList{i}
if any(ismember(pix(:), p)) && any(ismember(pix(:), q)) && any(ismember(V(:), p)) && any(ismember(V(:), q))
display('found N4 connectivity')
end
Actual Question: is this correct way to do the required task or am i doing it wrong?

MRT function [1] "error code = 0"

When I make my MRT, I got two errors:
[1] "error code = 0" and Error in indval.default(Ynode, clustering =
clustnode, numitr = 1000) : All species must occur in at least one
plot. Does anyone have an idea of why? I checked and all my species
have an abundance >0...
MRTtest=mvpart(vegetation~ Placette+ Tourb + Transect + Largcanal + Annouvert + Elevation + Profnappe + Litiere+ Solnu+ Deblign+ Densiometre+ EpaissMO+ Vonpostvingt+ Vonpostsoixante+ Pyrovingt+ Pyrosoixante+ Sommesurfterr,tot,margin=0.08,cp=0,xv="pick",xval=10,xvmult=150,which=4,pca=F)
X-Val rep : 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 26 27 28 29 30 31 32 33 34 35
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119
120 121 122 123 124 125 126 127 128 129 130 131 132 133
134 135 136 137 138 139 140 141 142 143 144 145 146 147
148 149 150
Minimum tree sizes
tabmins
2 3 4 6
2 125 5 18
MRTtest1=MRT(MRTtest,percent=10,species=colnames(vegetation))
summary(MRTtest1)
Portion (%) of deviance explained by species for every particular node
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- Node 1 ---
Complexity(R2) 14.87422
Sommesurfterr>=6.024 Sommesurfterr< 6.024
~ Discriminant species :
THOnmtot THOmtot
% of expl. deviance 17.61057298 38.419650816
Mean on the left 0.37621604 0.430818462
Mean on the right 0.08877576 0.006259911
[1] "error code = 0"
~ INDVAL species for this node: : left is 1, right is 2
cluster indicator_value probability
THOmtot 1 0.9597 0.001
THOnmtot 1 0.7878 0.001
LEG 1 0.5802 0.031
LIB 1 0.5078 0.010
MELnmtot 1 0.4710 0.047
EPNnmtot 1 0.4404 0.026
Sum of probabilities = 87.497
Sum of Indicator Values = 30.02
Sum of Significant Indicator Values = 12.67
Number of Significant Indicators = 29
Significant Indicator Distribution
1 2
8 21
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- Node 2 ---
Complexity(R2) 7.920283
Densiometre< 19.88 Densiometre>=19.88
~ Discriminant species :
TRA THOmtot
% of expl. deviance 10.54536819 27.8848051
Mean on the left 0.02754503 0.5158733
Mean on the right 0.20823621 0.2220475
Error in indval.default(Ynode, clustering = clustnode, numitr = 1000)
: All species must occur in at least one plot

How to regress Y on X using matlab?

Given :
Y=[81 55 80 24 78 52 88 45 50 69 66 45 24 43 38 72 41 48 52 52 66 89];
X=[124 49 181 4 22 152 75 54 43 41 17 22 16 10 63 170 125 15 222 171 97 254];
I want to regress Y on X (simple linear regression). I tried with this code :
b= regress(Y,X)
But it gives me this error :
??? Error using ==> regress at 65
The number of rows in Y must equal the number of rows in X.
Thanks for any help.
regress expect its inputs as column vectors.
Transposing (.') your inputs should do the trick:
>> b = regress( Y.', X.' )
b =
0.4291

How to reshape a matrix horizontally using MATLAB

I have matrix A of the size(4,192). It consists of 12 matrices of the size(4,4) aligned horizontally. I want to get matrix B with the size(12,16). B must get as follows:
suppose
A=[y1,y2,y3,...,y12]
in which yn is a 4*4 matrix. Then,
B=[y1,y4,y7,y10;
y2,y5,y8,y11;
y3,y6,y9,y12]
Is there an efficient/quicker (using no loop) way to do this using MATLAB?
You can try the following code:
ys1 = 2; % size(1) from submatrix (for the following example, use ys1 = 4 for the actual problem)
ys2 = 2; % size(2) from submatrix (for the following example, use ys2 = 4 for the actual problem)
ns1 = 3; % size(1) of final matrix in terms of submatrix (3 rows)
ns2 = 4; % size(2) of final matrix in terms of submatrix (4 columns)
temp = reshape(A,ys1,ys2,ns1,ns2);
B = reshape(permute(temp,[1 3 2 4]),ys1*ns1,ys2*ns2);
Example:
A = [11 12 21 22 31 32 41 42 51 52 61 62 71 72 81 82 91 92 101 102 111 112 121 122;
13 14 23 24 33 34 43 44 53 54 63 64 73 74 83 84 93 94 103 104 113 114 123 124];
B =
11 12 41 42 71 72 101 102
13 14 43 44 73 74 103 104
21 22 51 52 81 82 111 112
23 24 53 54 83 84 113 114
31 32 61 62 91 92 121 122
33 34 63 64 93 94 123 124