How to save matrixes as a vector - matlab

I have a text file that is a matrix 2819x10.
I have splited it into 5x5x563 matrix using code below
Matrix = dlmread('det.txt');
for j=1:1:563
for i=1:1:5
M(i,1,j) = Matrix(temp,3);
M(i,2,j)= Matrix(temp,4);
M(i,3,j)= Matrix(temp,5);
M(i,4,j) = Matrix(temp,6);
M(i,5,j) = 1;
temp=temp+1;
end
end
After this code I have Matrix 5x5x563. Right now I would like to to create a array like is presented bellow, that consist only one row, and each column is my matrix of 5x5.
I have tried with mat2cell:
MatrixNew= mat2cell(M, 5, 5);
But I have still an error. I dont have clue how to fix it. I am not try to find a ready code but just advice.
How can I accomplish this?

I think reshape should do the job for you. For example:
x=reshape(M,[1 5*5*563]);
or you can use other variations of the reshape function by playing around with it.

Related

Issue using imfill command in MATLAB

Hi I have a binary variable (called 'column' size 733x1) and I am trying to change the 0's in-between where I have 1's to 1's (i.e. 00001110011... to 00001111111...). I have tried using imfill, however have been unable to do so. I have converted it from type logical to uint8 to help but it hasn't worked.
column=column*255 % convert to form to work with 'imfill' command
column_fill=uint8(column)
column_fill=imfill(column);
However in between 1's within my variables I still have several 0's which I want to get rid of. Link to data. Output (from 000..000111000011101... to 000..000111111111111...) Thanks.
Try the following sample:
load data
column=column100*255 % convert to form to work with 'imfill' command
%Create column filled with 255 values.
white_column = ones(size(column))*255;
%Padd column from both sides (create 3 columns image).
im = [white_column, column, white_column];
%Apply imfill on image im
im_fill = imfill(uint8(im));
%Extract center column of im_fill.
column_fill = im_fill(:, 2);
You can also try the following code without using imfill:
column_fill = column100;
column_fill(find(column100 == 1, 1):find(column100 == 1, 1, 'last')) = 1;
You could use imfill with the 'holes' option
BW2= imfill(BW,'holes')
but you should leave your image binary and not multiply with 255.

Extract values from vector and save in new vector

I have a vector Cycle() that can contain several elements with a variable size.
I want to extract from this vector all the values which are in the odd columns, i.e. Cycle(1), Cycle(3), Cycle(5) ... and save them into a new vector Rcycle.
That's my code:
Rcycle = zeros(1, length(cycle)/2);
Rcycle(1) = cycle(1);
for j=3:length(cycle);
for i=2:length(Rcycle);
Rcycle(i) = cycle(j);
j = j+2;
end
end
Also I want to extract from Cycle() the even columns and save them in a vector Lcycle. My code:
Lcycle = zeros(1, length(cycle)/2);
Lcycle(1) = cycle(2);
for k=4:length(cycle);
for i=2:length(cycle);
Lcycle(i) = cycle(k);
k = k+2;
end
end
By running this for a sample Cycle() with 12 elements I get the right results for Lcycle, but the wrong ones for Rcycle. Also I get the error that my matrix have exceeded its dimension.
Has anyone any idea how to solve this in a more smooth way?
Use vector indexing!
Rcyle=cycle(1:2:end); %// Take from cycle starting from 1, each 2, until the end
Lcycle=cycle(2:2:end);%// same, but start at 2.

Foreach loop problems in MATLAB

I have the following piece of code:
for query = queryFiles
queryImage = imread(strcat('Queries/', query));
queryImage = im2single(rgb2gray(queryImage));
[qf,qd] = vl_covdet(queryImage, opts{:}) ;
for databaseEntry = databaseFiles
entryImage = imread(databaseEntry.name);
entryImage = im2single(rgb2gray(entryImage));
[df,dd] = vl_covdet(entryImage, opts{:}) ;
[matches, H] = matchFeatures(qf,qf,df,dd) ;
result = [result; query, databaseEntry, length(matches)];
end
end
It is my understanding that it should work as a Java/C++ for(query:queryFiles), however the query appears to be a copy of the queryFiles. How do I iterate through this vector normally?
I managed to sort the problem out. It was mainly to my MATLAB ignorance. I wasn't aware of cell arrays and that's the reason I had this problem. That and the required transposition.
From your code it appears that queryFiles is a numeric vector. Maybe it's a column vector? In that case you should convert it into a row:
for query = queryFiles.'
This is because the for loop in Matlab picks a column at each iteration. If your vector is a single column, it picks the whole vector in just one iteration.
In MATLAB, the for construct expects a row vector as input:
for ii = 1:5
will work (loops 5 times with ii = 1, 2, ...)
x = 1:5;
for ii = x
works the same way
However, when you have something other than a row vector, you would simply get a copy (or a column of data at a time).
To help you better, you need to tell us what the data type of queryFiles is. I am guessing it might be a cell array of strings since you are concatenating with a file path (look at fullfile function for the "right" way to do this). If so, then a "safe" approach is:
for ii = 1:numel(queryFiles)
query = queryFiles{ii}; % or queryFiles(ii)
It is often helpful to know what loop number you are in, and in this case ii provides that count for you. This approach is robust even when you don't know ahead of time what the shape of queryFiles is.
Here is how you can loop over all elements in queryFiles, this works for scalars, row vectors, column vectors and even high dimensional matrices:
for query = queryFiles(:)'
% Do stuff
end
Is queryFiles a cell array? The safest way to do this is to use an index:
for i = 1:numel(queryFiles)
query = queryFiles{i};
...
end

Creating matrix names iteratively in MATLAB and performing operations

I have a 3-Dimensional matrix K(i,j,l). I would like to create a new matrices from K, which would be a slice for each value of i. I also have to transpose the newly formed 2-D matrix.
for l=1:40
for j=1:15
K1(l,j)=K(1,j,l);
K2(l,j)=K(2,j,l);
.
.
.
K35(l,j)=K(35,j,l);
end;
end;
I want to create another loop where the names of new matrices are created within the loop.
i.e.;
K1(l,j)=K(1,j,l) (when i=1)
K2(l,j)=K(2,j,l) when i=2...
The problem that I faced is that I cannot seem to iteratively name of the matrices (K1,K2...K35) with in the loop and at the same time perform the dimension change operation. I tried num2str, sprintf, but they don't seem to work for some reason. If any of you have an idea, please let me know. Thanks!
I don't understand why you want to assign different names to your matrices. Can't you just store them in a cell like this:
K = cell(35, 1);
for ii=1:35
K{ii} = squeeze(K_DEA(ii, :, :))';
end
Otherwise, if you really need to have different names, then do this:
K = cell(35, 1);
for ii=1:35
eval(sprintf('K%d = squeeze(K_DEA(ii, :, :))'';', ii));
end
If I understand your question correctly, following should solve your problem:
K1=squeeze(K(1,:,:))';
K2=squeeze(K(2,:,:))';
.
.
.
K35=squeeze(K(35,:,:))';
For looping over i=1:35
for i=1:35
name = sprintf("K%d",i);
A = squeeze(K(i,:,:))';
eval([name ' = A']);
end
Or more concisely,
for i=1:35
eval([sprintf("K%d = squeeze(K(i,:,:))'",i)]);
end

copy certain rows in a new matrix within a loop

I'm trying to split a matrix in smaller matrices depending on one characteristic (i use 'if').
for jj = 1:length(FailureHoopUP_sorted)
if FailureHoopUP_sorted(jj,1)==20
FailureHoopUP_20(jj,:) = FailureHoopUP_sorted(jj,:);
elseif FailureHoopUP_sorted(jj,1)==30
FailureHoopUP_30(jj,:) = FailureHoopUP_sorted(jj,:);
else
FailureHoopUP_40(jj,:) = FailureHoopUP_sorted(jj,:);
end
end
The problem I have is that there are rows of zeroes that get in between the rows with data in the new created matrices.
I was wondering how i could avoid this?
Thank you for your help.
You don't need a loop, you can use logical indexing. For example:
FailureHoopUP_20=FailureHoopUP_sorted(FailureHoopUP_sorted(:,1)==20,:)
...
...
This should also solve the zeros issue (that happens because you keep the original index jj that is related to the length of FailureHoopUP_sorted).