I'm a noob in the world of Matlab and I want to append a single value in the beginning of all rows in a matrix.
mat =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
myval = 98;
I want to get a matrix like:
mat =
98 16 2 3 13
98 5 11 10 8
98 9 7 6 12
98 4 14 15 1
What should I do?
I know that we can use horizontal concatenation like [a b] but here myval and mat are not of the same dimension
I did this like:
m = ones(4,1)*98
m =
98
98
98
98
mat = [m mat]
mat =
98 16 2 3 13
98 5 11 10 8
98 9 7 6 12
98 4 14 15 1
Is there a better way ?
Thank you
Anothe one-line solution could be:
mat=[repmat(myval,size(mat,1),1) mat]
By using size(mat,1) you can handle automatically the number of rows.
hope this helps.
Qapla'
I think what you did is fine. You can one-line it by combining the statements:
mat = [(myval*ones(4,1)) mat];
You can use transpose (') with horizontal concatenation (horzcat or [x y]) as:
>> z = magic(4)
z =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> c = [98 98 98 98]
c =
98 98 98 98
>> [c' z]
ans =
98 16 2 3 13
98 5 11 10 8
98 9 7 6 12
98 4 14 15 1
>> horzcat(c', z)
ans =
98 16 2 3 13
98 5 11 10 8
98 9 7 6 12
98 4 14 15 1
Related
I want to create a new table extracting x rows every n rows from my table. For example if my table is:
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
I want something like:
1 2
3 4
11 12
13 14
21 22
Here is an easy way by using ismember and mod to categorize the rows
n = 5;
x = 2;
b = a(logical(ismember(mod(0:size(a)-1,n),0:x-1)),:);
such that
>> b
b =
1 2
3 4
11 12
13 14
21 22
Data
a = [1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22];
Hello I'm trying to do the following:
This is my table :
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
and I have a matrix [25,1].
I want to do the following: if the values in first and last columns match the numbers in the matrix, change the value to "99".
So the output should be this:
99
2
3
4
99
99
7
8
9
99
99
12
13
14
99
99
17
18
19
99
99
22
23
24
99
This is my attempt:
NT = zeros (x*y:1);
NT(:,1) = 1:x*y;
for i = 1:x*y
for j = 1
if NT(i,j) == x1(i,j)
NT(i,j) = 99;
end
end
end
This can be done very easily with ismember. Let
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];
B = (1:25).';
new_value = 99;
Then
B(ismember(B, A(:, [1 end]))) = new_value;
gives
B =
99
2
3
4
99
99
7
8
9
99
99
12
13
14
99
99
17
18
19
99
99
22
23
24
99
In one file 'file.mat', I have a matrix which its size is (1,100), it is written vertically like this:
M1 =
Columns 1 through 26:
6 13 3 15 13 12 8 5 5 1 11 8 5 9 1 7 15 9 2 5 7 7 3 9 0 13
Columns 27 through 52:
4 5 7 2 6 6 2 7 12 5 5 12 0 6 11 15 1 2 12 9 13 9 7 13 2 2
Columns 53 through 78:
7 15 4 15 5 12 5 12 14 3 10 15 12 5 5 15 3 3 9 3 6 0 13 13 8 5
Columns 79 through 100:
2 10 0 8 5 5 9 8 13 14 15 14 10 6 7 8 9 10 14 5 2 5
How to change it in an horizontal Matrix?
You can use M1.' or permute(M1,[2 1]). If you want all numbers to be in one horizontal line (i.e. to be an vector) you can use reshape(M1, [1,100])
What you have is a horizontal vector, but MATLAB displays it like that so that you can easily see where each element belongs. I guess what you want is to display the vector as a horizontal vector, so that you can copy-paste it. If so:
You can use sprintf if you want to display this as a long vector.
sprintf('%i ', M)
ans =
35 3 31 8 30 4 1 32 9 28 5 36 6 7 2 33 34 29 26 21 22 17 12 13 19 23 27 10 14 18 24 25 20 15 16 11
Or if you need the brackets:
['[', sprintf('%i ', M), ']']
ans =
[35 3 31 8 30 4 1 32 9 28 5 36 6 7 2 33 34 29 26 21 22 17 12 13 19 23 27 10 14 18 24 25 20 15 16 11 ]
You can also have it tab-separated: sprintf('%i\t', M), or with commas: sprintf('%i,', M).
If you want to reshape your horizontal vector to a vertical, you can do:
M = M.';
Note that ' is NOT the transpose operator, .' is. If you have a vector, but don't know whether it's horizontal of vertical, use the following notation: M = M(:).', or reshape(M, 1, []).
I have a 100x200 matrix and I would like to show this matrix as a density plot. Here is a 8x10 sample.
X = [104 122 138 159 149 167 184 164 190 158; ...
54 42 55 55 63 75 72 73 66 76; ...
15 22 28 21 23 28 32 47 32 40; ...
18 12 20 22 28 17 30 17 22 18; ...
10 7 14 10 14 11 14 20 16 10; ...
5 6 3 3 6 12 6 2 8 9; ...
4 8 9 2 5 3 3 12 7 7; ...
6 6 2 3 10 1 9 8 11 8]
I have tried to use functions like bar3, surf, hist and so on but they don't have the end result I am after.
I would also like to represent the y axis on the new successful plot to be on a log axis. So similar to having semilogy(x,y,'rx') for example.
Are there any other methods I could use?
How about "surf" it like a spectrogram?
XX = log([104 122 138 159 149 167 184 164 190 158;
54 42 55 55 63 75 72 73 66 76;
15 22 28 21 23 28 32 47 32 40;
18 12 20 22 28 17 30 17 22 18;
10 7 14 10 14 11 14 20 16 10;
5 6 3 3 6 12 6 2 8 9;
4 8 9 2 5 3 3 12 7 7;
6 6 2 3 10 1 9 8 11 8]
figure
surf(XX, 'edgecolor', 'none'); view(0,90); axis tight;
xlabel ('x')
ylabel ('y')
NOTE:The first row represent the first row (104,122,138...)
and row 8 represent row 8 (6,7,2....)
Dark red = high value
light blue = low value
Matlab also provides a heatmap function.
>> X = [104 122 138 159 149 167 184 164 190 158; ...
54 42 55 55 63 75 72 73 66 76; ...
15 22 28 21 23 28 32 47 32 40; ...
18 12 20 22 28 17 30 17 22 18; ...
10 7 14 10 14 11 14 20 16 10; ...
5 6 3 3 6 12 6 2 8 9; ...
4 8 9 2 5 3 3 12 7 7; ...
6 6 2 3 10 1 9 8 11 8];
>> heatmap(X)
ans =
HeatmapChart with properties:
ColorData: [8×10 double]
Show all properties
The following plot appears:
I am trying to get the intersection between two vectors but the index in both vectors should be the same. For example:
x = [1 2 3 4 5 6 7 80 9 100 11 12 103 14 150 16 170 18 20 19]
y = [22 1 3 40 5 4 70 8 90 10 110 12 13 140 15 160 17 18 19 20]
the intesection should be [3 5 12 18] only.
My code:
x = [1 2 3 4 5 6 7 80 9 100 11 12 103 14 150 16 170 18 20 19];
y = [22 1 3 40 5 4 70 8 90 10 110 12 13 140 15 160 17 18 19 20];
inter = intersect(x,y);
It's simple with logical indexing:
>> x = [1 2 3 4 5 6 7 80 9 100 11 12 103 14 150 16 170 18 20 19];
>> y = [22 1 3 40 5 4 70 8 90 10 110 12 13 140 15 160 17 18 19 20];
>> x(x==y)
ans =
3 5 12 18
>> x(abs(x-y)<=3) %// or y(abs(x-y)<=3) for the y values instead of the x values
ans =
2 3 5 6 12 18 20 19