export Matrix with this format MATLAB - matlab

How to export any size matrix like
A=
1 2 3 4 5 6 ....9
3 6 7 8 9 9 ....4
...
6 7 7 4 4 5 ... 2
To a file that will contain that matrix where each value is separated by ',':
1, 2, 3, 4, 5, 6, ....,9
3, 6, 7, 8, 9, 9, ....,4
...
6, 7, 7, 4, 4, 5, ... ,2

Use DLMWRITE. Doing this:
>> A = [1 2 3 4; 5 6 7 8];
>> dlmwrite('file.csv', A);
writes a file with this:
1,2,3,4
5,6,7,8

Related

how do I concatenate the results of a concatenated array?

I have two matrices (dfs):
A = [1 2 3 4
5 6 7 8
9 10 11 12]
and B = [1, 2, 3]
and I want matrix C to be repeating each row in A, B times. for example, first row, 1,2,3,4 needs to be repeated once, second row: 5,6,7,8 twice and last row three times:
C = [1 2 3 4
5 6 7 8
5 6 7 8
9 10 11 12
9 10 11 12
9 10 11 12]
my code
for i in range(0,2401):
g = pd.concat([df1.iloc[[i]]]*z[i], ignore_index=True)
partially does this, except only gives me the 3 times last row part, I need to concatenate each concatenation.
below gives me what I want but its not clean, i.e. indices are not ignored and messy.
result = []
for i in range(0,2401):
g = pd.concat([df1.iloc[[i]]]*z[i], ignore_index=True)
result.append(g)
If you write your matrices like:
A = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
B = [1, 2, 3]
C = []
You can iterate through B and add to C like:
for i in range(len(B)):
index = 0
while index < B[i]:
C.append(A[i])
index += 1
Which has an output:
[[1, 2, 3, 4],
[5, 6, 7, 8],
[5, 6, 7, 8],
[9, 10, 11, 12],
[9, 10, 11, 12],
[9, 10, 11, 12]]
I hope this helps!

How to check if a list of lists contains any of the elements from another list

I'm trying to make a sample lottery checker.
I'm using python.
x = [1,2,3,4,5,
y = [[1,2,3,4,5,6] # 6 numbers hit
,[1,2,3,4,6,7] # 5 numbers hit
,[2,3,4,6,7,8] # 4 numbers hit
,[4,5,6,7,8,9] # 3 numbers hit
,[1,2,7,8,9,10] # 2 numbers hit
,[4,7,8,9,10,11] # 1 number hit
,[7,8,9,10,11,12]]
output: (including the number of hits)
[1,2,3,4,5,6] 6 number hit
[1,2,3,4,6,7] 5 numbers hit
[2,3,4,6,7,8] 4 numbers hit
[4,5,6,7,8,9] 3 numbers hit
[1,2,7,8,9,10] 2 numbers hit
[4,7,8,9,10,11] 1 number hit
I tried using the any() function but only returned true or false.
please help.
Data:
x = [1,2,3,4,5,6]
y = [[1,2,3,4,5,6] # 6 numbers hit
,[1,2,3,4,6,7] # 5 numbers hit
,[2,3,4,6,7,8] # 4 numbers hit
,[4,5,6,7,8,9] # 3 numbers hit
,[1,2,7,8,9,10] # 2 numbers hit
,[4,7,8,9,10,11] # 1 number hit
,[7,8,9,10,11,12]]
Code:
for ticket in y:
print(ticket)
count = 0
for item in x:
if item in ticket:
count += 1
print(count, " numbers hit!")
Output:
[1, 2, 3, 4, 5, 6]
6 numbers hit!
[1, 2, 3, 4, 6, 7]
5 numbers hit!
[2, 3, 4, 6, 7, 8]
4 numbers hit!
[4, 5, 6, 7, 8, 9]
3 numbers hit!
[1, 2, 7, 8, 9, 10]
2 numbers hit!
[4, 7, 8, 9, 10, 11]
1 numbers hit!
[7, 8, 9, 10, 11, 12]
0 numbers hit!

Select only group of records for where condition

I have a table similar to this
THid Sid TID Sealantid
1 1 1 1
2 1 2 1
3 1 3 4
4 1 4 1
5 1 5 1
6 1 6 1
33 2 1 1
34 2 2 1
35 2 3 1
36 2 4 1
37 2 5 1
38 2 6 1
65 3 1 1
66 3 2 1
67 3 3 4
68 3 4 1
69 3 5 1
70 3 6 1
97 4 1 1
98 4 2 1
99 4 3 8
100 4 4 1
101 4 5 1
102 4 6 1
129 5 1 1
130 5 2 1
131 5 3 8
132 5 4 1
133 5 5 1
134 5 6 1
161 6 1 1
162 6 2 1
163 6 3 4
164 6 4 1
165 6 5 1
166 6 6 1
193 7 1 1
194 7 2 1
195 7 3 4
196 7 4 1
197 7 5 1
198 7 6 1
225 8 1 1
226 8 2 1
227 8 3 4
228 8 4 1
229 8 5 1
230 8 6 1
257 9 1 1
258 9 2 1
259 9 3 1
260 9 4 1
261 9 5 1
262 9 6 1
289 10 1 1
290 10 2 1
291 10 3 4
292 10 4 1
293 10 5 1
294 10 6 1
Here I wanted to find records only Sid's "where all sealantid=1"
Simple query I tried this
select * from table where sealantid=1
but this gives me all sid's but I want only SID's, Tid's (1 to 6) where all the sealantid=1
In this table 2 , 9
You can use NOT EXISTS with a subquery
Try this:
WITH SampleData AS (
SELECT V.*
FROM (VALUES
(1, 1, 1, 1)
,(2, 1, 2, 1)
,(3, 1, 3, 4)
,(4, 1, 4, 1)
,(5, 1, 5, 1)
,(6, 1, 6, 1)
,(33, 2, 1, 1)
,(34, 2, 2, 1)
,(35, 2, 3, 1)
,(36, 2, 4, 1)
,(37, 2, 5, 1)
,(38, 2, 6, 1)
,(65, 3, 1, 1)
,(66, 3, 2, 1)
,(67, 3, 3, 4)
,(68, 3, 4, 1)
,(69, 3, 5, 1)
,(70, 3, 6, 1)
,(97, 4, 1, 1)
,(98, 4, 2, 1)
,(99, 4, 3, 8)
,(100, 4, 4, 1)
,(101, 4, 5, 1)
,(102, 4, 6, 1)
,(129, 5, 1, 1)
,(130, 5, 2, 1)
,(131, 5, 3, 8)
,(132, 5, 4, 1)
,(133, 5, 5, 1)
,(134, 5, 6, 1)
,(161, 6, 1, 1)
,(162, 6, 2, 1)
,(163, 6, 3, 4)
,(164, 6, 4, 1)
,(165, 6, 5, 1)
,(166, 6, 6, 1)
,(193, 7, 1, 1)
,(194, 7, 2, 1)
,(195, 7, 3, 4)
,(196, 7, 4, 1)
,(197, 7, 5, 1)
,(198, 7, 6, 1)
,(225, 8, 1, 1)
,(226, 8, 2, 1)
,(227, 8, 3, 4)
,(228, 8, 4, 1)
,(229, 8, 5, 1)
,(230, 8, 6, 1)
,(257, 9, 1, 1)
,(258, 9, 2, 1)
,(259, 9, 3, 1)
,(260, 9, 4, 1)
,(261, 9, 5, 1)
,(262, 9, 6, 1)
,(289, 10, 1, 1)
,(290, 10, 2, 1)
,(291, 10, 3, 4)
,(292, 10, 4, 1)
,(293, 10, 5, 1)
,(294, 10, 6, 1)
) AS V (THid, Sid, TID, Sealantid)
)
SELECT DISTINCT SD.Sid
FROM SampleData AS SD
WHERE NOT EXISTS (
SELECT 1 FROM SampleData AS C
WHERE SD.Sid = C.Sid AND C.Sealantid <> 1
)
You can try it on fiddle
An alternative could be LEFT JOIN or NOT IN
SELECT DISTINCT SD.Sid
FROM SampleData AS SD
LEFT JOIN (
SELECT DISTINCT Sid FROM SampleData WHERE Sealantid <> 1
) AS C
ON SD.Sid = C.Sid
WHERE c.Sid IS NULL
SELECT DISTINCT SD.Sid
FROM SampleData AS SD
WHERE SD.Sid NOT IN (
SELECT DISTINCT Sid FROM SampleData WHERE Sealantid <> 1
)
You can use gruop by with having:
Select sid
From table
Group by sid
Having min(sealantId) = 1
And max(sealantId) = 1

Extract elements from matrix

How can I extract the elements: [1,2,5,6], [3,4,7,8], [9,10,13,14], [11,12,15,16] ?
A = [1, 2, 3, 4;
5, 6, 7, 8;
9, 10, 11, 12;
13, 14, 15, 16;];
I'm using octave.
Best regards, Chris.
If you need four matrices then use
out = mat2cell(A,[2 2], [2 2]);
If you need four vectors with values , then use
out = cellfun(#(x)(reshape(x,1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);
output will be
out{:,:}
ans =
1 5 2 6
ans =
9 13 10 14
ans =
3 7 4 8
ans =
11 15 12 16
Thanks, to Joe Serrano ,If you need the value in each of the four vectors in same order use,
out = cellfun(#(x)(reshape(x',1,[])),mat2cell(A,[2 2], [2 2]),'UniformOutput',0);
output will be
out{:,:}
ans =
1 2 5 6
ans =
9 10 13 14
ans =
3 4 7 8
ans =
11 12 15 16

Can I do max filter on a sub_blocks with matlab

For example, I have a 4x4 matrix, I want to split the max into 2x2 sub_regions. and do max on the sub_region.
1, 2, 3, 4
5, 6, 7, 8
9, 10, 11, 12
13, 14, 15, 16
\|/
6, 6, 8, 8
6, 6, 8, 8
14, 14, 16, 16
14, 14, 16, 16
Does this suit your need?
(Assuming your data is in a matrix called M)
>> cellfun(#(x) max(x(:)), mat2cell(M, [2 2], [2 2]))
ans =
6 8
14 16
EDIT:
You could also include kron to achieve your desired output:
>> kron(cellfun(#(x) max(x(:)), mat2cell(M, [2 2], [2 2])), ones(2))
ans =
6 6 8 8
6 6 8 8
14 14 16 16
14 14 16 16
colfilt will get the job done:
>> M = 2; N = 2;
>> B = colfilt(A,[M N],'distinct',#(x)repmat(max(x),[M*N 1]))
B =
6 6 8 8
6 6 8 8
14 14 16 16
14 14 16 16
The key is to use the 'distinct' block type option. Test data: A = reshape(1:16,4,4).'.
You can also use blockproc if you prefer:
B = blockproc(A,[M N],#(b) repmat(max(b.data(:)),[M N]))
OR
B = kron(blockproc(A,[M N],#(b) max(b.data(:))),ones(M,N))
Note: Image Processing Toolbox required for both.