Converting matrix into 128x128 picture - matlab

I am pretty new to Matlab, and I am trying to convert my data file into an
128x128 matrix in order to display an image. So, in my file I have 3 columns with 16384 numeric values in each of them, and I need to have 128x128x3 matrix, like a format of image. I was trying method reshape, but it did not work for me, I am getting an error such
Error using reshape
To RESHAPE the number of elements must not change.
Here is my code
x = load('out.txt');
B = reshape (x,128,128);
What would be the best solution for this problem?

If you want to make a 128x128x3 matrix, you have to say so to reshape:
B = reshape (x,128,128,3);
You can leave one of the values out, but you have to replace it with an empty array:
B = reshape (x,128,[],3);
This will calculate the size for that dimension.

Related

Matlab: reshape 4-d matrix to 2-d and maintain order, how to?

I'm trying yo implement vlsh with the California ND Datastet, wich is composed by 701 photos.
10 subject wrote down in a txt file which photos are near duplicate for them, and we have also correlation matrix.
The images are RGB and i reduced them in 20x20. I created a 4-d array 20x20x3x701. So i tried to reshape and obtained a 1200x701 matrix, but the problem is that reshape can't maintain the order of the original matrix.
I tried to search online and most of suggestion is to use "Permute", but it seems to me that doesn't fit my situation.
I can post the matlab code:
`
path='C:\Users\franc\Desktop\stage\californiaND\prova*.jpg';
path2='C:\Users\franc\Desktop\stage\californiaND\prova';
d=dir(path);
a=[];
for m=1:length(d)
a=cat(4,a,imread(strcat(path2,d(m).name)));
end
r=reshape(a,[],701);
r=double(r);
L = lshConstruct( r, 10,4);`
I am assuming you need the 1D vector in RGBRGB format, as otherwise the solution would be trivial ( just (:)).
Assuming imread is reading 20x20x3 images one by one, this is how you directly make your 2D matrix:
for m=1:length(d) % this is 701, right?
im=imread(strcat(path2,d(m).name));
im=permute(im,[3 1 2]);
a=cat(2,im(:));
end

Submatrix based on size vector

It seems like this problem should be common, but I haven't found a good duplicate...
I'm implementing a level 2 S-function with a variable-sized multidimensional output. The state has to be in fixed-size Dwork vectors, so I zero-pad the input matrix to the maximum size allowed for the input and then reshape it to a vector.
When I reshape it back to a matrix for output, I need to trim it back down to the correct size.
The function needs to be general enough to support an arbitrary number of dimensions. The size of the output is stored in a size array.
For example, I may have a 500x500 matrix N, and a size array S = [40 25]. I need a MATLAB expression that would give me N(1:S(1), 1:S(2)), but it needs to work for any number of dimensions so I can't simply hardcode it like that.
Here is a solution in m-code:
%your input
M=rand(10,10,10);
S=[2,3,4]
%generate indices:
Index=arrayfun(#(x)(1:x),S,'uni',0)
%use comma separated list to index:
smallM=M(Index{:})

Putting vectors of different size in a matrix

I am trying to add a number of vectors in a Matrix where each row represents a vector, but it gives me "Subscripted assignment dimension mismatch." error. The main problem is that each vector has a different size. I tried to add zeros at the end of the short vectors but I couldn't do it. Any Help.
Example:
%signal is a vector of data.
[x(1,:),y(1,:)] = findpeaks(signal1);
[x(2,:),y(2,:)] = findpeaks(signal2); %error as the peaks count in signal 2 is not the same as in signal 1.
OK, given two vectors of unequal length,
A=rand(1,10)
B=rand(1,5)
the proper way to deal with this is to use a cell array
D={A;B}
And then you can get whatever elements you want like this, for example:
D{1}(1:3) %// A(1:3)
If you don't want to use cells, you can add rows using this little function that adds row vector M to matrix F
addRow=#(F,M) [F NaN(size(F,1),size(M,2)-size(F,2));M NaN(1,size(F,2)-size(M,2))]
you would use it like this:
F=A
F=addRow(F,B)

Reshaping a 3D data in matlab

I have this 3-D data in matlab of dimension 3x3x10. What I want is to reshape it to a data of size/dimension 10x9. Such that for each i,j,1:10. I have one column in this new data. How can I do it. I tried using reshape(data, 10,9). and it gave me a data structure of size 10x9. However, I doubt how it arranged it.
I want to reshape it just that if new_data is my new data of size 10x9, my first column is old_data(1,1,:). The second column of my new data is old_data(1,2,:) and so on
I personally abhor the use of for loops in Matlab. I think a better way would be to permute the dimensions, then reshape. Also, it is not clear the order in which you want to arrange the data. You should be able to achieve what you want by fiddling with the dimension numbers in the permute function call.
% B is a 3x3x10, permute to 10x3x3, then reshape into 10x9
% Change the ordering of the dimension in permute to achieve the desired result
% remember: reshape takes elements out of B column by column.
newB = reshape(permute(B,[3 1 2]),10,9);
% newB is a 10x9 matrix, where each row is the entries of the 3x3 matrix
% the first column is element 1:9:82
You could do this:
for i = 1:size(x,3)
x(:,:,i) = x(:,:,i).';
end
newData = reshape(x,[],size(x,3)).'

Invalid index error in Scilab when trying to access array element

I'm not sure why I can't do this in Scilab.
-->foo=zeros(500);
-->foo(300)
!--error 21
Invalid index.
Why do I get the 'Invalid index' error? I thought I had initialized foo as an array with 500 elements, each of which was set to 0?
In Scilab, you have to give both the number of rows as well as the number of columns. So, if you want to create a 500x500 matrix, you need to say zeros(500, 500). If you want a 500x1 vector, you need to say zeros(500, 1).
If you want to create a zeros matrix that has precisely as many rows and columns as another matrix (say A), you need to say zeros(A). This is where the confusion stems from.
In Scilab, zeros(500) would take 500 as a 1x1 matrix and generate a zeros matrix of size 1x1, that is [0]. In MATLAB, zeros(500) would take 500 to be the size of the matrix required, assuming a square matrix.
If zeros in Scilab behaves just like zeros in Matlab the call zeros(500) creates a 500x500 array of 0s. That said, foo(300) would be a valid Matlab expression as Matlab understands what it calls 'linear indexing' on arrays of rank greater than 1.
If zeros in Scilab does bot behave just like zeros in Matlab I can't help.
printf("%d\n",Md(y,u))
!--error 21
Índice inválido.
at line 69 of exec file called by :
como soluciono esto?