cell2mat produces wrong output size - octave

I have a cell matrix with size 32x32 where each cell is an 8x8 matrix and all values are floats. I use DCT to each 8x8 cell and after that I assign each matrix to another empty cell matrix. So, the new cell matrix is 32x32 as expected and when I use cell2mat with said matrix, I get size 556x556 instead of 256x256.
first_arr=mat2cell(inputArr,8*ones(1,size(img,1)/8),8*ones(1,size(img,2)/8),1);
c=cell(32);
for i=1:32
for j=1:32
temp=cell2mat(first_arr(i,j));
temp1=dct(temp);
for k=1:8
for l=1:8
if(some_condition)
temp1(k,l)=0;
endif
endfor
endfor
inverted=idct(temp1);##inverted dct
c(i,j)=inverted;
endfor
endfor
temp2=cell2mat(c);
After the execution I have the following results:
size(c)=32x32
size(temp2)=556x556
If I use cell2mat with first_arr the resulted size is 256x256.
Can somebody explain why that is happening?
I tried everything and I can't seem to find a solution to that. I checked the sizes of each matrix on every step and everything is 8x8 as expected

Related

Simulink block to generate points from equation

I need help in Simulink, I am looking for a way to generate an array as the outputs from Simulink block like if t=0:1:10s then x = 2sin(t), I know only of the block that gives constant value of x. This is the model block:
Highlighted blocks give constants; I want to replace them with any block that would generate an array using equation such as instead of x=50 it will give x=2sin(t)
I have found the answer. The block I was looking for is 'fcn'. It is only available in Matlab 2020a and below, that's why I couldn't find it (I have 2021a). Hope it helps anybody else.
This is what it looks like, you can just write your equation in it

Interpolating specific data on Matlab

I am trying to analyze an image sequence of a twisting ribbon (helicoidal structure) by measuring its pitch angle but I am facing some problems in the extraction of my results.
Basically what I am trying to do is building a code that calculate the pitch angle of the helix obtained experimentally : what I have done is : calculate the width of the ribbon at every value of the height and deduce the radius of the helix. The code itself works properly and it is shown below :
%% We go to the specific file that we want to treat
clear all
path = rdir('9V\largeur.mat');
load(path{1})
%% We define the spatial scaling
% We have a picture (echelle.tiff) that tells us 10cm=565pix
echelle=565/.1; %(pixel per meter). It's the frequency
%
% for z = 1:size(largeur,1)
%
% hauteur(z) = z/echelle; %Converting the height of the image to cm
%
% end
hauteur=[1:size(largeur,1)]/echelle;
for t = 1:size(largeur,2)
signal_spatial=largeur(:,t)/echelle;
signal_spatial=(signal_spatial-min(signal_spatial));
radius = max(signal_spatial); % The radius corresponds to the maximum of the ribbon's width
theta(:,t)=acos(signal_spatial/radius); % theta = acos(x/R)
% figure(3)
% title('Angle theta en fonction de la hauteur')
% Plotting the angle theta as a function of the height z
%
%hold on
%plot(hauteur,signal_spatial)
%xlabel('hauteur (cm)')
%ylabel(['theta(z,t=',num2str(t),')'])
%title(num2str(t))
% hold off
end
%cd('6V');
%save('theta','theta');
%cd ..
plot(hauteur,theta(:,401)) % we plot for the last image ranked 401
After that, I plot the values :
plot(hauteur,-theta(:,t),'r')
And I obtain this result :
And this is my problem : now I want to get rid of some branches in order to obtain the linear evolution of the angle as a function of the height: typically my goal is to "select" the first red branch and the second blue one in order to obtain a kind of "straight" line but I don't know how to do it ?
I have thought about trying to do it by working on the fact that the wanted branches correspond to the "increasing" part of the respective functions but how to do so ?
Could you suggest a way to do it please ?
Thank you !
PS : any suggestions please ?

Simulink Matlab function block deleting rows from a vector

That i want to do is to delete certain rows (or columns doesn't really mater...) from a given vector.
By going through Simulink's components found out that there is nothing performing such an operation,there are blocks help one add elements but nothing clearly for removing,so ended up trying to delete them by using a function block and following the online examples that demonstrate the usage of "[]".Lets say that i want to delete the second column of the vector u,i do u(:, 2) = [];.
That works absolutely fine in a separate m file or function but unfortunately not in a function block returning:
"Simulink does not have enough information to determine output sizes for
this block. If you think the errors below are inaccurate, try specifying
types for the block inputs and/or sizes for the block outputs."
and:
Size mismatch (size [4 x 4] ~= size [4 x 3]).
The size to the left is the size of the left-hand side of the assignment.
Function 'MATLAB Function' (#107.41.42), line 4, column 1:
"u"
Launch diagnostic report.
Is there any alternative you can suggest to remove several elements in a given vector in Simulink?
Thanks in advance
George
Finally,managed to do it without function block.There is a much easier way,by using Pad,and defining the output vector to be shorter than the input resulting in truncation.

Plotting a histogram using a csv file

I have a csv file with the following format.
Label 1, 20
Label 2, 10
Label 3, 30
.
.
.
LabelN, 5
How do I plot the second column using the labels given in the csv file as labels on the x-axis?
(Something like this, where 1891-1900 is a label)
EDIT:
Found these questions which are quite similar to mine,
Plotting word frequency histogram using gnuplot
Gnuplot xticlabels with several lines
After trying the commands given in answer 1.
set xtics border in scale 1,0.5 nomirror rotate by -90 offset character 0, 0, 0
plot "data.txt" using 2:xticlabels(1) with histogram
I'm getting a not so clean histogram because the number of labels is quite large. I've tried the formatting given in answer 2. Can anyone suggest a way to get a cleaner histogram?
You have several options:
Plot only the important labels (extremes, mean etc. for example)
Skip every 5th label or so if labels form a series
Split your graph if you must plot every single label.
Seems like case 2) applies here, and thus skipping some of the labels before plotting will make the plot look better.
You can pre-process the file to skip every 5th label (say) using something like the following script:
line_number = 0
for line in open("d1.txt", "r"):
line_split = line.split(",")
if(line_number % 5 == 0):
print line,
else:
print ",",line_split[1],
line_number += 1
You can now plot with appropriate font size
set xtics border in scale 1,0.5 nomirror rotate by -90 offset character 0, 0, 0
set xtics font ",9"
plot "d2.txt" using 2:xticlabels(1) with histogram title "legend_here"

Freefem++: Solving poisson equation with numerical function

I am using Freefem++ to solve the poisson equation
Grad^2 u(x,y,z) = -f(x,y,z)
It works well when I have an analytical expression for f, but now I have an f numerically defined (i.e. a set of data defined on a mesh) and I am wondering if I can still use Freefem++.
I.e. typical code (for a 2D problem in this case), looks like the following
mesh Sh= square(10,10); // mesh generation of a square
fespace Vh(Sh,P1); // space of P1 Finite Elements
Vh u,v; // u and v belongs to Vh
func f=cos(x)*y; // analytical function
problem Poisson(u,v)= // Definition of the problem
int2d(Sh)(dx(u)*dx(v)+dy(u)*dy(v)) // bilinear form
-int2d(Sh)(f*v) // linear form
+on(1,2,3,4,u=0); // Dirichlet Conditions
Poisson; // Solve Poisson Equation
plot(u); // Plot the result
I am wondering if I can define f numerically, rather than analytically.
Mesh & space Definition
We define a square unit with Nx=10 mesh and Ny=10 this provides 11 nodes on x axis and the same for y axis.
int Nx=10,Ny=10;
int Lx=1,Ly=1;
mesh Sh= square(Nx,Ny,[Lx*x,Ly*y]); //this is the same as square(10,10)
fespace Vh(Sh,P1); // a space of P1 Finite Elements to use for u definition
Conditions and problem statement
We are not going to use solve but we ll handle matrix (a more sophisticated way to solve with FreeFem).
First we define CL for our problem (Dirichlet ones).
varf CL(u,psi)=on(1,2,3,4,u=0); //you can eliminate border according to your problem state
Vh u=0;u[]=CL(0,Vh);
matrix GD=CL(Vh,Vh);
Then we define the problem. Instead of writing dx(u)*dx(v)+dy(u)*dy(v) I suggest to use macro, so we define div as following but pay attention macro finishes by // NOT ;.
macro div(u) (dx(u[0])+dy(u[1])) //
So Poisson bilinear form becomes:
varf Poisson(u,v)= int2d(Sh)(div(u)*div(v));
After we extract Stifness Matrix
matrix K=Poisson(Vh,Vh);
matrix KD=K+GD; //we add CL defined above
We proceed for solving, UMFPACK is a solver in FreeFem no much attention to this.
set(KD,solver=UMFPACK);
And here what you need. You want to define a value of function f on some specific nodes. I'm going to give you the secret, the poisson linear form.
real[int] b=Poisson(0,Vh);
You define value of the function f at any node you want to do.
b[100]+=20; //for example at node 100 we want that f equals to 20
b[50]+=50; //and at node 50 , f equals to 50
We solve our system.
u[]=KD^-1*b;
Finally we get the plot.
plot(u,wait=1);
I hope this will help you, thanks to my internship supervisor Olivier, he always gives to me tricks specially on FreeFem. I tested it, it works very well. Good luck.
The method by afaf works in the case when the function f is a free-standing one. For the terms like int2d(Sh)(f*u*v), another solution is required. I propose (actually I have red it somewhere in Hecht's manual) an approach that covers both cases. However, it works only for P1 finite elements, for which the degrees of freedom are coincided with the mesh nodes.
fespace Vh(Th,P1);
Vh f;
real[int] pot(Vh.ndof);
for(int i=0;i<Vh.ndof;i++){
pot[i]=something; //assign values or read them from a file
}
f[]=pot;