# Questions tagged [matlab]

24431 questions

1

votes

2

answer

3.4k

Views

### MATLAB: n-minute/hour/day averages of a time-series

This is a follow-up to an earlier question of mine posted here. Based on Oleg Komarov's answer I wrote a little tool to get daily, hourly, etc. averages or sums of my data that uses accumarray() and datevec()'s output structure. Feel free to have a look at it here (it's probably not written very wel...

1

votes

1

answer

781

Views

### Matlab's Quiver3 plots only 4 vectors

I want to use the command quiver3(X,Y,Z,M,N,O)
in order to get a vector field, where all matrices are 10x10x10 arrays with real entries. Now I wanted to plot it and got only 4 vectors instead of my expected 1000. Does anybody here know, what I could possibly have done wrong?
If you need further info...

1

votes

1

answer

497

Views

### Extract low-frequency coefficients of Fourier transformation

I have applied the Fourier transformation on an image like so:
I = imread('img.jpg');
img = fftshift(I);
F = fft2(img);
magnitude = mat2gray(100*log(1+abs(fftshift(F)))); % Magnitude spectrum
phase = mat2gray( (angle(F)) ); % Phase spectrum
Using the energy compaction proper...

1

votes

1

answer

2.3k

Views

### Sheet music using MATLAB GUI

I'm working on a project to automatically transcribe piano music (monophonic). I thought of creating a MATLAB GUI which could display the sheet music of the transcribed music. However I'm not quite sure how I could design the Grand Staff on MATLAB GUI. This is my first time using the GUI and would r...

1

votes

2

answer

82

Views

### Matlab - create new matrix from combining two other

I have
two matrices:
A = [1 2; 3 4; 2 6; 4 7];
B = [2 4; 4 4; 4 3; 5 8];
How can I create new matrix, that it would have order like, first A row, then first B row, then second A row, then second B row etc.
Like this:
C = [1 2; 2 4; 3 4; 4 4; 2 6; 4 3; 4 7; 5 8];

1

votes

1

answer

594

Views

### Cosine distance range interpretation

I am trying to use the cosine distance in pdist2. I am confused about it's output. As far as I know it should be between 0 and 1. Since MATLAB uses 1-(cosine), then 1 would be the highest variability while 0 would be the lowest. However the output seems to range from 0.5 to 1.5 or something along th...

1

votes

1

answer

40

Views

### replacing a particular number of an array by the number of that position of another array

I am trying to write a function to replace a particular number of an array by the number of that position of another array. However, it does not allow me to do so. I am quite new in using MATLAB. Any help will be appreciated.
arr1 = [
1 3 8 9
2 47 10 4
2 4 6 86
6...

1

votes

3

answer

98

Views

### Unique cell vectors

I try to find unique arrays in a cell array. Suppose I have 6 cells with the following vectors:
a{1}=[1 2];
a{2}=[1 2 3];
a{3}=[2 3 4];
a{4}=[1 2];
a{5}=[1 2 3];
a{6}=[2 3 4];
Then the result should be [1 2], [1 2 3] and [2 3 4]. I used u=(cellfun(@unique,a,'Un',0)), but it doesn't work, How can I d...

1

votes

2

answer

1.5k

Views

### Summing over first 2D of 3D matrix

Suppose I have a 3D matrix A of size (1,2,3). Something like
A(:,:,1) =
[1 2]
A(:,:,2) =
[3 4]
A(:,:,3) =
[5 6]
How would I go about finding the 3 values that make up the sum of the first two dimensions, for example.
sum(A(:,:,1) = 3
The above works, but when I try it for an array with larger dime...

1

votes

1

answer

38

Views

### error substracting two date numbers in matlab

Im using matlab and I have the following problem. I have two date vectors:
A{1,1} = '25-02-2011 0:03:32';
A{2,1} = '25-02-2011 0:03:32';
A{3,1} = '25-02-2011 0:03:32';
A{4,1} = '25-02-2011 0:03:32';
A{5,1} = '25-02-2011 0:03:32';
And
B{1,1} = '24-02-2011 23:57:36';
B{2,1} = '24-02-2011 23:48:44';
B{...

1

votes

2

answer

977

Views

### Calculate percentage of zeros and ones in my vector?

I've already have my vector and number of zeros and ones with this code:
u=[1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0]
transitions=(find(u~=[u(2:end), u(end)+1]));
value=u(transitions)
transitions(2:end)=transitions(2:end)-transitions(1:end-1)
i get this
value =
1 0 1 0 1...

1

votes

2

answer

1.1k

Views

### Subtract background from image

I am trying to to subtract the background from a picture of an object to leave only the foreground object. I have found the RGB values of the background as 218 220 219 using imshow(). How can I use the RGB values with imsubtract()?
y = [218 220 219];
z = imsubtract(img,y);
Error using imsubtract (li...

1

votes

2

answer

98

Views

### Matlab vectorization of for loops

Is there any way to vectorize such a for loop in MATLAB? It's taking a lot of time to execute.
for i = 1:numberOfFrames-1
frameDifferencesEroded(:,:,i+1) = imabsdiff(frameDifferencesErodedTemp(:,:,i+1),frameDifferencesErodedTemp(:,:,1));
for k=1:numel(frameDifferences(1,:,i))
for m=1:numel(frameDiff...

1

votes

2

answer

1.8k

Views

### Finding maximum/minimum distance of two rows in a matrix using MATLAB

Say we have a matrix m x n where the number of rows of the matrix is very big. If we assume each row is a vector, then how could one find the maximum/minimum distance between vectors in this matrix?

1

votes

2

answer

822

Views

### Generate Asymmetric Triangular wave using Modulo function in single Line ? for MATLAB

y=abs(a-mod(x,(a+1)));
a=0 for sawtooth
a=1 for symmetric Triangle
https://flic.kr/p/oCJSHp
and I needed what function for following image using single line modulo function
https://flic.kr/p/oCJs9V

1

votes

1

answer

1.2k

Views

### Overlay line plot with shaded bar areas in matlab

I want to overlay my line plot in Matlab with colored bars for different x-axis values. The functionality I'm looking for is much like recessionplot(), but it should not indicate NBER recessions, but user defined periods. I'm able to do this with the following code, but the color of my original line...

1

votes

1

answer

752

Views

### hierarchical classification with SVM

I am trying to deal with a problem of classification with SVM, at the beginning I managed to solve the problem at the first level, ie classify my data into 2 classes (class1 and class2). now I want to continue the classification hierarchically ie f I want to separate the second class into two cla...

1

votes

2

answer

1.6k

Views

### How to generate a video file using a series of plots on MATLAB?

I'm trying to stitch together a bunch of plots I created within a loop into a single video file. I've been at this for several hours, but had no luck. Here is my minimum working example where I attempt to use the VideoWriter function to create a video. I always get an error saying my frame(s) can't...

1

votes

1

answer

6.7k

Views

### fminsearch multiple parameters matlab

I'm trying to use fminsearch with multiple parameters but I can't seem to even get it working with two. I've also tried using optimization tool in matlab but then I get:
Optimization running.
Error running optimization.
Not enough input arguments.
What i do:
fval = fminsearch(@g,[1 1])
The function...

1

votes

1

answer

35

Views

### undefined result during formatting in hexadecimal form

I can't understand what is reason of following result
>> format hex
>> 10
ans =
4024000000000000
>> 12
ans =
4028000000000000
as i know this numbers should be written in hexadecimal format,but why such result?i have tried different variant for example like this
>> x=20;
>> format hex
>> x
x =
40340...

1

votes

1

answer

42

Views

### Finding X binary numbers with n non-zero digits for large X. - Matlab

Is there a more efficient method for generating X binary numbers (that have n non-zero digits) for a range of 1 to N? I have developed the following solution:
Totalcombos = nchoosek(N,n);
floor = floor(log2(Totalcombos));
L = 2.^floor;
NumElem = 2^N-1;
i=0;
x=1;
%Cre...

1

votes

2

answer

70

Views

### Dividing a array and storing in matrix

I have an array nMean_2 (1-by-16320), I want to divide it into 255 equal parts and store it in matrix (64-by-255)
mean2Electro = zeros(64,255);
r=1;
for z=1:64
mean2Electro(z,:)= nMean_2(r:((z*256)-1))
r= z*256;
end
But, I am getting an error:
??? Subscripted assignment dimension mismatch.
in mean2...

1

votes

2

answer

2.9k

Views

### Matlab fill contour3 plot like contourf plot

I'd like to fill a 3D contour plot (contour3(X,Y,Z)) like the 2D contour fill plot (contourf(X,Y,Z)). But I can't figure out how this can be achieved. The combination of contour3 and surf is not very satisfying, since there are tiles.
[X,Y,Z] = peaks(32);
figure
contourf(X,Y,Z,15);
figure
contour3(X...

0

votes

0

answer

6

Views

### Can I extract ldf file database (LIN file) like dbc files (CAN file) when I use canDatabase?

Actually I am trying to extract database from LIN (ldf file) in MATLAB and I do not know how. For example, MATLAB already has a function called canDatabase and if you give it an input file of dbc, it gives you arrays with names of signals etc. I am trying to do the same with LIN means I give a file...

1

votes

1

answer

270

Views

### Generate a Gaussian Image without adding noise

I want to generate a Gaussian Image by Matlab. It has 3 circles (as three classes). The intensity in each circle will be followed by Gaussian distribution. Thus, the histogram of the image will be multiplicate Gaussian distribution as a question. However, I used a free-noise image and added it with...

1

votes

1

answer

478

Views

### MATLAB - passing parameters to pdist custom distance function

I've implemented a custom distance function for k-medoids algorithm in Matlab, following the directions found in pdist.
Basically it compares two vectors, say A and B (which can also have different lengths) and checks if their elements "co-occur with tolerance": A(i) and B(j) co-occur with toleranc...

-1

votes

0

answer

8

Views

### MATLAB 3X3 Convolution Filter returns lines at 45 deg

Can you help me find a 3x3 convolution filter, which when applied to an image, only returns the lines at 45 deg from the x-axis. In other words, the convolution filter should return high values when applied on a line drawn at 45 deg from the x-axis, and return low values otherwise. Using the filter,...

1

votes

2

answer

41

Views

### Writing a loop that works for any value of N

I have a code for a 1D heat equation. Im trying to format a for loop so that the A matrix will follow a certain pattern of 1 -2 1 down the entire diagonal of a matrix that could be infinite. The pattern starts to take shape when I mess around with the initialized count at the beginning of the for lo...

1

votes

1

answer

36

Views

### Matlab: How to specify input in matlabFunction?

matlabFunction() is a function that can convert symbolic to anonymous function. But how to specify what input arguments to be appeared on the anonymous function?
For example,
x = sym('x', [3, 1])
func = matlabFunction(x)
It returns a handle with:
func =
function_handle with value:
@(x1,x2,x3)[x1;x2...

0

votes

0

answer

3

Views

### Load of Transfer Learning on GPU (Matlab)

I've been trying the transfer learning examples provided by mathworks with my own datasete (105 different labels) on the pretrained models matlab provided.
When training, I noticed that the Training Progress window dows say 'Hardware Resourse : Single GPU' and I've seen the 'Cuda library needs to be...

0

votes

0

answer

6

Views

### MATLAB matrix dimension adjustment - automatically

I have a matrix where the array have mismatched sizes. for example, the first column has 50000 rows, the second column has 48200 rows and as such makes upto 30 columns in the matrix. After importing the matrix I want a square matrix if arrays with all arrays having the exact same size of the smalles...

-1

votes

2

answer

2k

Views

### Find index of a pair of value in array with matlab

I've 2 values and I would like to find them in a array.
This values have the same index
Example:
0 0
1 2
2 3
4 5
I'm looking for (1,2) so the index is 2

1

votes

2

answer

705

Views

### fast way to convert datetime to string

I want to know if there is faster way to convert a datetime to a string besides datestr.
datetime is inserted every other lines in my main function (including all of its dependency). I need time at that line of code is executed.
I think my only option is to convert datetime to string faster....

0

votes

0

answer

2

Views

### Comparing the result of LSTM and a deep neural network

Can I use sequenceInputLayer(n_features) when applying deep neural net and NOT LSTM model in Matlab?
I applied LSTM on time-series data in Matlab 2018b. Now, I want to show that considering temporal relationships in LSTM improves performance. To do that, I need to apply a deep neural net with the sa...

1

votes

1

answer

1.3k

Views

### Python 2.7: Area opening and closing binary image in Python not so accurate

I am using Python 2.7 and I used following Python and Matlab function for removing noises and fill holes in this image
.
1. Code to remove noise and fill holes using Python and Opencv
img = cv2.imread("binar.png",0)
kernel = np.ones((5,5),np.uint8)
open = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel...

1

votes

3

answer

1.4k

Views

### Plotting sin(x)/(x) in Matlab

I am having issues plotting sin(x)/(x) correctly. Specifically when x = 0, returns NaN in Matlab. However, when applying L'Hôpital's rule the actual value is y = 1. My question is how can I go into the y matrix and change the of NaN. What I brainstormed for possible approaches is creating a For loo...

1

votes

1

answer

153

Views

### How to plot bars reversed up with negative values?

Starting by this 10x3 y matrix, where all its entries are negative values:
y = [-56 -58 -60;
-75 -74 -72;
-66 -66 -69;
-67 -69 -71;
-66 -67 -71;
-59 -58 -57;
-69 -71 -69;
-59 -58 -58;
-66 -67 -68;
-75 -73 -72];
I need to plot it simply using bar(y), but the ba...

1

votes

2

answer

915

Views

### Converting python Dataframe to Matlab file

I am trying to convert a python Dataframe to a Matlab (.mat) file.
I initially have a txt (EEG signal) that I import using panda.read_csv:
MyDataFrame = pd.read_csv("data.txt",sep=';',decimal='.'), data.txt being a 2D array with labels. This creates a dataframe which looks like this.
In order to con...

1

votes

2

answer

254

Views

### Plotting confidence intervals in Matlab

I am having trouble plotting confidence intervals through errorbar function inside Matlab. I have written the following code below
clear all;
close all;
%CH15 Program for Chapter 15
%
% Monte Carlo for a European call
randn('state',100)
%%%%%%%%%%%%%%%%% Problem and method parameters %%%%%%%%...

1

votes

1

answer

29

Views

### Storing p values using Econometrics toolbox in matlab

This code will run with the econometrics toolbox,
model = arima('Constant',0.5,'AR',{0.9999},'Variance',.4);
rng('default')
Y = simulate(model,50);
figure
plot(Y)
xlim([0,50])
title('Simulated AR(1) Process')
rng('default')
Y = simulate(model,50,'NumPaths',1000);
Y1=Y(:,1);
for ii = 1:50
Mdl = arim...