Column - Most frequent letter in a group of 4 rows - matlab

I have this column in excel;
V
V
F
V
C
F
F
F
...
Now I'm reading it with matlab using
[~,txt] = xlsread('2012_15min.xls','JAN','B25:B2999');
And now I want to get a new column that gives me the most repetitive letter in groups of 4 rows, so for the first 4 rows I will get V (in this example), and for the second F.
So I will get a new column with;
V
F
...
I hope you can help me.

You can use the command mode to find the most frequent occurence. The only cavity is that mode does not work with chars. So, you can reshape txt to be of size 4-by-whatever and then find the mode of each 4-column
>> res = char( mode( double( reshape( txt, 4, [] ) ) ) ).'
res =
V
F

Related

Trying to solve an error problem with Matlab v 9.13 when doing matrix multiplication

Here is the problem and current code I am using.
Use the svd() function in MATLAB to compute , the rank-2 approximation of . Clearly state what is, rounded to 4 decimal places. Also, compute the root-mean square error (RMSE) between and . Which approximation is better, or ? Explain.
Solution:
%code
[U , S, V] = svd(A)
k = 2
V = V(:,1:k)
V = transpose(V)
Ak = U(:,1:k) .* S(1:k,1:k) .* V
diffA = A - A2
fro_norm = norm(diffA,'fro')
RMSE2 = (fro_norm)/sqrt(m*n)
However, when running, the line AK = . . . keeps giving an error because the matrix sizes are not compatible. So I understand that the matrix sizes need to match in order to do the multiplication, but I also know that the problem requires the following calculation requirements, that when k = 2, U has to use the first 2 columns, S has to use the first 2 rows and first 2 columns, and V has to be the transpose of V only using the first two columns.
I must be missing something in my understanding of the calculation, or creation of the sub k matrices. The matrix I have to use is a 3 x 3.

Matlab - matrix addition in for loop

I have a 7x21 matrix called A. Within this matrix there are three equally sized 7x7 submatrices. I call them B,C and D, where B = A(:,1:7), C = A(:,8:14) and D = A(:,15:21).
How can I produce a matrix E which is also 7x7 matrix where simply B, C and D are added up, i.e. E = B+C+D.
Thanks a lot for your help!
Generic code to get such an output -
N = 3; %// Number of submatrices
[m,n] = size(A) %// Get size [no. of cols must be multiple of N
E = reshape(sum(reshape(A,m*n/N,[]),2),m,n/N)
I don't see what's going to be more straightforward and concise than
E = A(:,1:7) + A(:,8:14) + A(:,15:21)
Unless you need an expression that generalizes in some way you're not describing...

Efficient way of mapping similar inputs to similar outputs

Is there a efficient way of approaching this particular problem in matlab.
I am trying to map this matrix or possible array BeansRice (see below)
Beans={0:1,0:1,0:2,0:2,0:2,0:2,0:1,0:1,0:2,0:2}
[a b c d e f g h i j ] = ndgrid(Beans{:})
BeansRice = [a(:) b(:) c(:) d(:) e(:) f(:) g(:) h(:) i(:) j(:)]
into a matrix/array BR (see below)
BR=[abc, de, fg, hij];
where if columns a, b and c each have values 0 (ties preference), I have preference for c>b>a. If all columns a, b and c each have values 1 (ties no preference), BR(1)=1. If columns a and b have values 0 and column c has value 2, BR(1)=2. If columns a and b have values 1 and column c has value 2, BR(1)=1.
I have an if function with indexing but I was thinking if it is possible to improve it, using the rank/order of the values in the matrix to break ties. Looking for a more efficient process as this is only a sub of a large problem.
You can use logical indexing instead of if conditions. For example
BR1(a==1 & b==1 & c==1)=1
BR1(a==0 & b==0 & c==2)=2
BR1(a==1 & b==1 & c==2)=1
...
then process the other parts, BR2(d==... & e>...)=##, then concatenate to obtain what you need
BR=[BR1(:) BR2(:) ...]
etc...

bsxfun rows subtraction in loop

I have a text file D with 98 rows and 2 columns. looks like this:
10 0.261344
11 0.456167
12 0.668595
2 0.481754
... etc
I have another excel file M with 17 rows and 2 col.
I want to subtract all rows values from the 1st row, then from 2, etc.so i get something like this:
10-11 -0.194823
10-12 -0.407251
... etc
in other words difference between 1st row and all rest, 2nd row and all rest ..excluding those rows of which 1st column has same values as in 2nd M excel file 1st column. which makes in total difference compare to 81 rows.
i have been trying to use this:
M = xlsread('...');
FSumID=fopen(D);
MatrixSub = loadD);
m = textscan(FSumID,'%d %f');
horzcat(m{:})
NewData = M(:,1)
D_size=size(D);
for i=1:D_size
if (ptvBodyDist(:,1)~=NewData(i))
%for calculating
DVec = bsxfun(#minus, ((m{i,2})), (m{i,2}));
%when i check this it gives only one loop all zeros(98,1)while i need (((98-17)^2,1). I need to change,last part of expression to get what i want but I'm stuck with bsxfun. i dont know how to make correct expression to subtract all rows.
I'm newbie in Matlab any help would be much appreciated. also if you could suggest some manuals or tutorials,apart from mathworks help. Many thanks.
Let D be n by 2 matrix (data from text file).
Let M by m by 2 matrix (data from excel file).
The first stage is to find the relevant n-m rows of D: rows that their first value does not appear in M, using setdiff.
[~, idx] = setdiff( D( :, 1 ), M( :, 1 ), 'stable' );
Now we can compute the all-vs-all diff using bsxfun
d = bsxfun( #minus, permute( D( idx, : ), [1 3 2] ), ...
permute( D( idx, : ), [3 1 2] ) );
Now d is |idx|x|idx|x2 array where
d( ii, jj, : ) = D( idx(ii), :) - D( idx(jj), : )

Extra and misaligned rows when importing data in SAS

I am trying to import data in SAS like:
A B C D E
x y z h i
s1 s2 s3 s4 s5
where A B C D and E are column names.
I have 240 columns in my dataset and the code I am using is:
data INFO;
infile Attdata notab dlm='09'x dsd missover LRECL = 100000000;
length A B C D E $200; (I am importing 240 columns)
input A B C D E;
run;
Whenever I import data, some of the rows of column B, C etc, gets stacked below rows of A:
A B C D E
x h i
s1 s2 s3 s5
y s4
z
Is there a way to fix this? Do I need to do something with lrecl one? My data gets all weird after running this code. Might there be problem with length one?
It may be something to do with missover (rather than LRECL). I have found this site to be useful in the past: http://www2.sas.com/proceedings/sugi26/p009-26.pdf
Are you saying in your question that data from line 1 is appearing on lines 3 and 4 and some data from line 2 is on line 3? I've never seen SAS do this before.
You may want to check your delimiter/end of line characters.