I have an array of vectors:
array = [0 0 0 0 0 0 1
0 1 1 1 0 1 0
1 1 1 1 0 0 0
.............
.............]
and I want to print it into a file as it is:
0000001
0111010
1111000
....
....
etc.
I have this but it does not seem to work:
myoutput = fopen('c:\\aitest_file.txt', 'wt');
fprintf(myoutput, '%f\n', VAA_final);
fclose(myoutput);
dlmwrite('c:\aitest_file.txt', VAA_final, 'delimiter', '');
You need to transpose your output matrix and use the appropriate number of integer identifiers:
>> VAA_final = [0 0 0 0 0 0 1; 0 1 1 1 0 1 0; 1 1 1 1 0 0 0]
VAA_final =
0 0 0 0 0 0 1
0 1 1 1 0 1 0
1 1 1 1 0 0 0
>> myoutput = fopen('aitest_file.txt', 'wt');
>> fprintf(myoutput, '%u%u%u%u%u%u%u\n', VAA_final');
>> fclose(myoutput);
Related
I have
x=[ 1 1 1 1 0 0 1 1 1 0 1 0 0 0 0 0 0 1 0 1]
I want to find all the regions that have more than 5 zeros in a row. I want to find the index where it starts and where it stops.
In this case I want this: c=[12 18]. I can do it using for loops but I wonder if there is any better way, at least to find if there are some regions where this 'mask' ( mask=[0 0 0 0 0] ) appears.
A convolution based approach:
n = 5;
x = [0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0];
end_idx = find(diff(conv(~x, ones(1,n))==n)==-1)
start_idx = find(diff(conv(~x, ones(1,n))==n)==1) - n + 2
returning
end_idx =
6 14 25
start_idx =
1 9 20
Note that this part is common to both lines: diff(conv(~x, ones(1,n))==n) so it would be more efficient to pull it out:
kernel = ones(1,n);
convolved = diff(conv(~x, kernel)==n);
end_idx = find(convolved==-1)
start_idx = find(convolved==1) - n + 2
You can use regexp this way:
convert the array into a string
remove the blanks
use regexp to find the sequence of 0
A possible implementation could be:
x=[ 1 1 1 1 0 0 1 1 1 0 1 0 0 0 0 0 0 1 0 1]
% Convert the array to string and remove the blanks
str=strrep(num2str(x),' ','')
% Find the occurrences
[start_idx,end_idx]=regexp(str,'0{6,}')
This gives:
start_idx = 12
end_idx = 17
where x(start_idx) is the first element of the sequence and x(end_idx) is the last one
Applied to a more long sequence, start_idx and end_idx results being arrays:
x=[0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0]
start_idx =
1 9 20
end_idx =
6 14 25
How can I build an array given the requirements below?
for an array NxM of A(i,j):
for A(1,1), A(1,2), A(1,3) = 1 and A(1,4), A(1,5), A(1,6) = 0, repeat these 6 characters for A(1,M-5), A(1,M-4), A(1,M-3) = 1 and A(1,M-2), A(1,M-1), A(1,M) = 0.
for A(2,1), A(2,2) = 1 and A(2,3), A(2,4), A(2,5), A(2,6) = 0, repeat these 6 characters for A(2,M-5), A(2,M-4) = 1 and A(2,M-3) A(2,M-2), A(2,M-1), A(2,M) = 0.
for A(3,1) = 1 and A(3,2), A(3,3), A(3,4), A(3,5), A(3,6) = 0, repeat these 6 characters for A(3,M-5) = 1 and A(2,M-4), A(3,M-3), A(3,M-2), A(3,M-1), A(3,M) = 0
Repeat the above 3 steps for N rows
i.e for a 12x12 array
A = [1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0;
1 1 1 0 0 0 1 1 1 0 0 0;
1 1 0 0 0 0 1 1 0 0 0 0;
1 0 0 0 0 0 1 0 0 0 0 0]
I am working with iterative methods, and thus with large sparse matrices.
For instance, I want to set up a matrix like this:
1 1 0 0 1 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0
0 1 1 1 0 0 1 0 0 0
0 0 1 1 1 0 0 1 0 0
1 0 0 1 1 1 0 0 1 0
0 1 0 0 1 1 1 0 0 1
So that only certain diagonals are non-zero. In my programming, I will be working with much larger matrix sizes, but Idea is the same: Only a few diagonals are non-zero, all other entries are zeros.
I know, how to do it in for loop, but it seems to be not effective, if the matrix size is large. Also I work with symmetric matrices.
I would appreciate, if you provide me a code for my sample matrix along with description.
You want spdiags:
m = 6; %// number of rows
n = 10; %// number of columns
diags = [-4 -1 0 1 4]; %// diagonals to be filled
A = spdiags(ones(min(m,n), numel(diags)), diags, m, n);
This gives:
>> full(A)
ans =
1 1 0 0 1 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0
0 1 1 1 0 0 1 0 0 0
0 0 1 1 1 0 0 1 0 0
1 0 0 1 1 1 0 0 1 0
0 1 0 0 1 1 1 0 0 1
I'm looking for a vectorized solution for this problem :
Let A a vector (great size : > 10000) of 0 and 1.
Ex :
A = [0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1 etc]
I want to replace the 0 between the 1's (of odd ranks) by 2
i.e. to produce :
B = [0 0 0 1 2 2 2 2 2 1 0 0 0 1 2 2 1 0 0 1 2 1 etc]
Thanks for your help
It can be done quite easily with cumsum and mod:
A = [0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 1 0 1]
Short answer
A( mod(cumsum(A),2) & ~A ) = 2
A =
0 0 0 1 2 2 2 2 2 1 0 0 0 1 2 2 1 0 0 1 2 1
You requested to fill the islands of odd rank, but by changing mod(... to ~mod(... you can easily fill also the islands of even rank.
Explanation/Old answer:
mask1 = logical(A);
mask2 = logical(mod(cumsum(A),2))
out = zeros(size(A));
out(mask2) = 2
out(mask1) = 1
try using cumsum
cs = cumsum( A );
B = 2*( mod(cs,2)== 1 );
B(A==1) = 1;
I have cell array
Columns 1 through 6
[8x8 uint8] [8x8 uint8] [8x8 uint8] [8x8 uint8] [8x8 uint8] [8x8 uint8]
Columns 7 through 8
[8x8 uint8] [8x8 uint8]
if I use cell2mat function, I get this
Columns 1 through 18
0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0
0 1 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0
0 1 1 1 0 1 0 1 1 0 1 1 0 1 0 1 0 1
0 1 1 0 0 1 0 0 1 0 1 0 0 0 0 0 0 1
0 1 1 1 1 0 0 1 1 0 0 0 1 1 0 1 0 0
0 1 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0
0 1 1 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1
0 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 1
Columns 19 through 36
1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 0
1 1 0 1 1 0 0 0 0 1 1 1 0 1 0 0 0 0
0 0 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0
1 1 0 1 1 1 0 0 0 1 1 0 0 1 0 1 1 1
1 1 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0
0 1 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 0
0 0 0 0 1 0 0 0 0 1 1 1 0 1 0 1 1 1
Now I want matrix with 8 columns.
What I want is this
0 1 0 1 0 0 1 0
0 1 1 1 0 0 1 1
0 1 1 1 0 1 0 1
0 1 1 0 0 1 0 0
0 1 1 1 1 0 0 1
0 1 1 0 1 1 1 1
0 1 1 0 1 1 0 1
0 1 1 0 0 1 0 0
0 0 1 0 0 0 0 1
1 1 0 0 0 1 0 1
1 0 1 1 0 1 0 1
1 0 1 0 0 0 0 0
1 0 0 0 1 1 0 1
1 0 1 1 0 0 0 1
1 1 1 1 0 1 0 1
1 1 0 0 1 1 0 1
.
.
.
.
.
If I got your question correctly, you simply need to transpose the cell array before transforming it. See the following example (I edited the actual output to compress the display a bit):
> a
a =
{
[1,1] =
1 0 0
0 1 0
0 0 1
[1,2] =
2 0 0
0 2 0
0 0 2
[1,3] =
3 0 0
0 3 0
0 0 3
}
> cell2mat(a)
ans =
1 0 0 2 0 0 3 0 0
0 1 0 0 2 0 0 3 0
0 0 1 0 0 2 0 0 3
> cell2mat(a')
ans =
1 0 0
0 1 0
0 0 1
2 0 0
0 2 0
0 0 2
3 0 0
0 3 0
0 0 3
Note that using reshape brings another ordering:
> reshape(cell2mat(a), 9,3)
ans =
1 2 3
0 0 0
0 0 0
0 0 0
1 2 3
0 0 0
0 0 0
0 0 0
1 2 3
Just transposing your cell array and then passing it to cell2mat will probably be enough.
Another (less preferred, loops are generally not welcome in MATLAB) solution is to loop over your cell array and use matrix concatenation. If your cell array has name ca, this will do the thing:
imat = []; for i = 1:numel(ca); imat = [imat; ca{i}]; end
The answer will be in imat.
You can use the reshape function.
tmp = cell2mat(...);
res = reshape(tmp, numel(tmp)/8, 8);