I want to convert the contents of column 1 in a cell array into specific numbers as mentioned in the code but doesn't work.
col=1;
[row_patho,col_patho]=size(data);
for i=1:1:row_patho
if Pro_patho_data{i,col}>=70
Pro_patho_data(i,col)=11;
end
end
Input:
73 t2b 7
59 t1c 9
58 t3b 7
83 t3b 8
wanted_output:
11 t2b 7
59 t1c 9
58 t3b 7
11 t3b 8
Related
I want to select a control group from one data frame based of matching the age from a second data frame. As an example I have subject.df
subject.df
id age
1 1 55
2 2 62
3 3 73
4 4 54
5 5 66
I'd like to subset control.df based off of matching the age directly on a 1 to 1 matching from the subject.df dataframe.
control.df
id age
6 6 66
7 7 71
8 8 80
9 9 51
10 10 55
11 11 56
12 12 77
13 13 62
14 14 64
15 15 73
16 16 67
17 17 54
18 18 75
19 19 77
20 20 78
21 21 53
22 22 64
23 23 83
24 24 61
25 25 77
I'm fairly new to R. In the past I've used Matlab and in this instance would use a for loop to iterate over the control.df dataframe, but I've been told that R doesn't always like for loops and that it can be computationally difficult in R.
In the end I'll be doing this on a much larger data set where the subject group is around 250 and the control group is more than 40K so I know that 1:1 matching is possible.
I have the following matrix:
L = [3 6 18 92 2
2 24 39 59 3];
I intend to enter the first row of matrix L into the 2nd row of the following matrix:
X = [2 7 43 52 1
4 21 14 97 4
3 17 27 85 5];
And the result should be:
Xnew = [2 7 43 52 1
3 6 18 92 2
4 21 14 97 4
3 17 27 85 5];
How can I do that in Julia?
This is a way to do it:
julia> #views [X[1:1, :]; L[1:1, :]; X[2:end, :]]
4×5 Matrix{Int64}:
2 7 43 52 1
3 6 18 92 2
4 21 14 97 4
3 17 27 85 5
You could get the same without #views but it would be less efficient as it would create intermediate copies of data.
I have two .dat files. They are world.dat and sensor_data.dat. I have a folder name in D: drive named tutorial. Within this tutorial file, there are two folders data and code. Now in the data folder, there are two files as I mentioned earlier world.dat and sensor_data.dat. In the code folder, there is a file name main.m as it is a Matlab file.
The code that is written on this file(main.m) is
clc;
clear;
close all;
% Read *.dat files containing landmark data
landmarks = fopen('../data/world.dat');
landmarks_data = fread(landmarks);
% Read *.dat files containing odometry and range-bearing sensor data
data = fopen('../data/sensor_data.dat');
data_data = fread(data);
But when I print landmarks_data and data_data they print something other than that is written on those two files(world.dat,sensor_data.dat)
world.dat file contains:
1 2 1
2 0 4
3 2 7
4 9 2
5 10 5
6 9 8
7 5 5
8 5 3
9 5 9
My output:
>> landmarks_data
landmarks_data =
49
32
50
32
49
10
50
32
48
32
52
10
51
32
50
32
55
10
52
32
57
32
50
10
53
32
49
48
32
53
10
54
32
57
32
56
10
55
32
53
32
53
10
56
32
53
32
51
10
57
32
53
32
I don't know where they get those data? The same thing happened for data_data variable.
Need help to fix the problem.
You are getting the ASCII values of the characters in the file.
ASCII value of 1 equals 49.
ASCII value of ' ' (space) equals 32.
ASCII value of 2 equals 50...
fread reads data from binary file, and you are using fread for reading a text file. The binary value of a text character is the ASCII code (it can also be a UNICODE value).
In case you want to read the data as text, and keep the matrix structure, you can use readmatrix function:
landmarks = readmatrix('../data/world.dat');
Result:
landmarks =
1 2 1
2 0 4
3 2 7
4 9 2
5 10 5
6 9 8
7 5 5
8 5 3
9 5 9
Remark: In case your MATLAB version is before R2019a, you can use dlmread instead.
I have some data:
P = [3 10 25 32 43 1 3
6 12 35 39 49 4 9
2 9 23 36 47 2 9
...
7 20 35 42 44 3 7
15 18 19 41 42 4 6
10 18 32 35 46 3 10];
Data is always between 1 and 50.
I am selecting left 5 columns and 2 right columns:
L=P(:,1:5);
R=P(:,6:7);
I am counting occurrences:
a=tabul(L);
b=tabul(R);
In this moment, in a I am getting:
50. 3.
49. 4.
48. 3.
which tells me, that value 50 occurs 3 times, 49 occurs 4 times and so on.
What I need now is sort matrix a by second column but the first column should be arranged with the second column values. So it would look like this:
49. 4.
50. 3.
48. 3.
How can I sort matrix a this way (later I will sort b the same way)?
I was trying something like:
[a,idx]=gsort(a(:,2),"g","d");
a=a(idx,:);
but this not does what I need.
It does not work because you are overwriting a in the gsort call although you just need the index here. The following does what you want:
[dummy,idx]=gsort(a(:,2),"g","d");
a=a(idx,:);
Suppose we have a 2D (5x5) matrix:
test =
39 13 90 5 71
60 78 38 4 11
87 92 46 45 35
40 96 61 17 1
90 50 46 89 63
And a second 2D (5x2) matrix:
tidx =
1 3
2 4
2 3
2 4
4 5
And now we want to use tidx as an idex into test, so that we get the following output:
out =
39 90
78 4
92 46
96 17
89 63
One way to do this is with a for loop...
for i=1:size(test,1)
out(i,:) = test(i,tidx(i,:));
end
Question:
Is there a way to vectorize this so the same output is generated without a for loop?
Here is one way:
test(repmat([1:rows(test)]',1,columns(tidx)) + (tidx-1)*rows(test))
What you describe is an index problem. When you place a matrix all in one dimension, you get
test(:) =
39
60
87
40
90
13
78
92
96
50
90
38
46
61
46
5
4
45
17
89
71
11
35
1
63
This can be indexed using a single number. Here is how you figure out how to transform tidx into the correct format.
First, I use the above reference to figure out the index numbers which are:
outinx =
1 11
7 17
8 13
9 19
20 25
Then I start trying to figure out the pattern. This calculation gives a clue:
(tidx-1)*rows(test) =
0 10
5 15
5 10
5 15
15 20
This will move the index count to the correct column of test. Now I just need the correct row.
outinx-(tidx-1)*rows(test) =
1 1
2 2
3 3
4 4
5 5
This pattern is created by the for loop. I created that matrix with:
[1:rows(test)]' * ones(1,columns(tidx))
*EDIT: This does the same thing with a built in function.
repmat([1:rows(test)]',1,columns(tidx))
I then add the 2 together and use them as the index for test.