# Questions tagged [matlab]

33801 questions

0

votes

0

answer

4

Views

### Finding all occurences of a string within a cell array (itself part of a struc)

I have the following structure
dataDens =
dens: [1x172 double]
level: {1x172 cell}
raga: {1x172 cell}
within which dataDens.raga consists of (reducing the number of columns below for simplicity)
Columns 1 through 3
'Multani' 'Tori' 'Tori'
I'd like to find the indices at which 'Tori' appears...

1

votes

1

answer

561

Views

### PCA for dimensionality reduction MATLAB

I have a feature vector of size [4096 x 180], where 180 is the number of samples and 4096 is the feature vector length of each sample.
I want to reduce the dimensionality of the data using PCA.
I tried using the built in pca function of MATLAB [V U]=pca(X) and reconstructed the data by X_rec= U(:,...

0

votes

0

answer

3

Views

### How to set the size of the figures the same for my paper in Matlab?

I am writing a paper and it includes several figures. I wanna set the size of all figures the same in the paper since it's visually better. How can I do that? I thought it would be better to plot a figure first, and then get the size and position using command pos = get(gcf, 'Position'), and set the...

1

votes

1

answer

71

Views

### using Matlab fsolve() to find the zero points of 2 function with 2 variables

im using Matlab to trying to solve 2 equations with 2 variables.
I define the 2 functions, f2(n_1,n_2),f3(n_1,n_2) which both depend on f1(n_1,n_2), then I defined the vectorised function G(n_1,n_2) which contain both of them.
Later I defined a the desired stating point, and tried to solve. but when...

2

votes

3

answer

69

Views

### Sum daily values for every month in matlab

I have got a matrix with values in.
The first column of the matrix is the date in the following form, 19260701 pr YYYYMMDD.
The other columns of the matrix are series.
19260702 0.026 0.000 NaN 1.175
19260706 0.009 0.000 NaN 1.842
19260707 1.388 0.001 NaN 9.061
19260708 1.14...

1

votes

2

answer

44

Views

### Dynamically change property attributes

There are dependent properties in class A, based on an argument in constructor I want to make some of properties Hidden, so that user will not be able to set/get these properties.
classdef A
properties (Dependent = true)
prop1
prop2
end
methods
function value = get.prop1(obj)
...
end
function value...

1

votes

1

answer

21

Views

### Matlab string concatenation behavior with different data types

I am using Matlab 2018b on Windows 10. I understand that the Matlab string concatenation needs input arguments under concatenation to be a character array, a cell array of character vectors, or a string array as mentioned in https://de.mathworks.com/help/matlab/ref/strcat.html. Based on this i was e...

1

votes

1

answer

17

Views

### Fast way of concatenating complex data using vertcat with custom rows

I need to concatenate few bigger matrices, but in specific manner - for example to concatenate only 1 row from X matrices.
There is a good solution of storing the data in a structure, so then there is no need of preparing long list of things that needs to be concatenated.
For example, we will have a...

1

votes

1

answer

69

Views

### Does symbol capitalization matter in object files with a linked dll?

I'm trying to get the gfortran compiler working in MATLAB on Windows to create mex files. Gfortran isn't supported, but the Intel Fortran compiler is, which leads me to believe that a Fortran compiler should be able to compile Fortran source using MATLAB libraries.
As detailed in my previous questio...

1

votes

1

answer

46

Views

### Change size of figure in MATLAB to use them in LaTeX

I have generated several plots in MATLAB and I haven't set the ( width or height) to any of them. I save them as pdf to place them in LaTeX but each figure appear with different size. I can change each figure size in LaTeX but I wonder if there is an easy way in MATLAB to get same size for all figur...

1

votes

1

answer

43

Views

### Create structs from array elements based on index and value

Is there a more elegant way to express the following code (e.g. without explicit for-loop)?
P = [0.1 0.2 0.3 0.4];
% pre-allocate symbols array of struct
symbols = repmat(struct('probability', 0, 'indices', []), length(P), 1);
for i =1:length(P)
symbols(i) = struct('probability', P(i), 'indices', i)...

-2

votes

0

answer

15

Views

### How do I write this python code as a matlab one?

So I have made a code that calculates the circulation around 3 squares indirectly as a surface integral.
f1=0
for i in range(35,70):
for j in range(160,170):
f1 += (vx[j][i]-uy[j][i]) * 0.5
print(f1)
proceed to do the same with the 2 other squares.
but I want to also write this in MatLab.
f1 = 0
f...

2

votes

0

answer

12

Views

### Solving eigenvectors/eigenvalues of sparse matrix in different ranges successively

I'm looking for eigenvalues/eigenvectors of a huge sparse matrix A (250k*250k) that lie in different eigenvalue ranges without calculating all the eigenvalues in between since it would just take too much time.
My consideration was
Step1:
I'm trying to find k certain eigenvalues/eigenvectors of the m...

1

votes

1

answer

3.5k

Views

### how to run matlab script in mac terminal [closed]

I search for this and got some answer involving sudo, I am a beginner to linux and curious how to do it in terminal,
say my file is in Desktop/hw
matlab is installed in application
could give me a detailed commands instructions?
Thanks

1

votes

0

answer

10

Views

### Using loops for Selection dialog box (if, elseif, else)

I am trying to create a conditional loop based on the answers from my selection dialog box in Matlab. But i dont know the actual code for it.
Tried some trial and error but no luck.
list = {'Raw water','Treated Water','Sample water'};
[indx,tf] = listdlg('PromptString','Select water type:','Selecti...

1

votes

1

answer

2.1k

Views

### Sorting array of objects by property in MATLAB? [duplicate]

This question already has an answer here:
Matlab - sort cell array of objects by property
1 answer
I have an array of objects, each with various properties, and I would like to figure out how to run 'sort' on the array, specifying a specific property of each object to sort by.
For example, say my...

0

votes

0

answer

3

Views

### Matlab/Simulink: How to programmatic edit the Solar Cell component?

I am trying to programmatically add components to a Simulink model. The programmatic model editing logic of Matlab is very straight-forward, but here I run into an issue.
I am struggeling with the thermal port of the Solar Cell component (Simscape/Physical component). Manually, you can show the ther...

1

votes

2

answer

408

Views

### How do I continue the input to fprintf on the next line?

Given the following code:
fprintf('test test',...
'test');
I get the following output on my terminal:
test test
How can I continue on to the next line for fprintf?

1

votes

2

answer

2.1k

Views

### -bash: matlab: command not found

I am a bit surprised, but I can't run matlab by the terminal of my Mac.
I am trying the command
/Applications/MATLAB_R2015b.app/bin/matlab
but I get the error
-bash: matlab: command not found
Listing the contenents of the folder /Applications/MATLAB_R2015b.app/bin/ I can see the word matlab which is...

1

votes

2

answer

407

Views

### Adding poisson noise to an image with specific mean.

How do I add Poisson noise with a specific mean of Poisson distribution in Matlab? The code below gives me a completely white image.
img = im2double(phantom(512));
figure; imshow(img)
mean = 1e9;
R = poissrnd(mean, [512, 512]);
J = img + R;
figure; imshow(J);

1

votes

1

answer

38

Views

### How to build filenames in a nested loop

I am trying to access 4k images and crop some ROI based areas (4 ROI in my case) and store them in some directory. So far everything is working okay except the loops handling of the filename.
Below is my code attached. I am accessing N 4k images, crop and resize them to my desired resolution. In the...

1

votes

1

answer

50

Views

### Brackets around multiple outputs in Python function

In matlab, if we have a function that returns multiple variables, we do something like
[output1, output2] = some_func()
In Python, you can simply do
output1, output2 = some_func()
Or you could do
[output1, output2] = some_func()
Or
(output1, output2) = some_func()
The last 2 makes some temporary li...

1

votes

2

answer

43

Views

### Total number of pages in a PDF document

MATLAB provides the extractFileText function which allows us to read text from PDF files, among other file formats, and save the extracted text as a string.
We can pass an extra argument to this function in order to extract text from specific pages of the document.
For example, to extract the text f...

1

votes

1

answer

50

Views

### Create quarterly dates vector for plot in Matlab

How can I transform this (much longer) vector into quarterly values like Q1-1991 etc. that plot nicely on a graph in Matlab? I googled/Matlab-help-searched/searched in this forum and didn't find anything.
Dates = [19961; 19962; 19963; 19964; 19971; 19972]

1

votes

1

answer

68

Views

### How to solve an error about drawcircle in Matlab?

In the following code, that is for draw a circle on an image, an error occurred. I try to solve it but I can't find the problem.
figure('color', [1 1 1]);
clf;
imshow(inputVol(:,:,85),[]);
drawcircle([blobSlice(ind,2),blobSlice(ind,1)],round(sqrt(3)*blobSlice(ind,4)),50,'-','g', 1); % Error
For Ins...

1

votes

2

answer

31

Views

### Converting multiple .txt files to .mat in the same folder

I have many .txt files that contain n rows and 7 columns each delimited with whitespace. I want to convert each file to .mat file and save that in the same folder.
I tried this but it's not working:
files = dir('*.txt');
for file = files'
data=importdata(file.name);
save(file.name, 'data');
end
Whil...

1

votes

1

answer

49

Views

### Subplot two images with a vertical separator

I'm trying to create a figure with an image on the left (original image) and an image on the right (warped image) and a vertical line separating them, like this:
I've tried this by creating axes, without ticks and labels. Then drawing a line from bottom to top and applying hold on and finally subplo...

1

votes

3

answer

74

Views

### Output 1, 0.5, or 0 depending if a matrix elements are prime, 1, or neither

I am sending a matrix to my function modifikuj, where I want to replace the elements of the matrix with:
1 if element is a prime number
0 if element is a composite number
0.5 if element is 1
I dont understand why it is not working. I just started with MATLAB, and I created this function:
function B...

1

votes

2

answer

58

Views

### How to fit my data into an exponential model but with an extra constant in matlab

Im very new to matlab, and i don't know how to fit my datapoints into a model of my choice.
My datapoints are not completely exponential, and i would like to fit my data into a model like this: y=a*e^(bx)+c, since the data won't go against 0 for large x-values, but are going to a constant, c that i...

1

votes

1

answer

30

Views

### function that replaces the negative numbers of an array with 77

function Z=replacez(A)
[M,N]=size(A);
for i=1:M
for j=1:N
if A(i,j)

1

votes

2

answer

50

Views

### Finding unique elements of a MATLAB cell array when elements are like '(p,q)'

I have a MATLAB cell array like this:
a = {'(q0, q1)' '(q2, q3)' '(q1, q0)' '(q4, q5)'};
'(q0, q1)' and '(q1, q0)' are indifferent in my application, So I want to eliminate one of them. What is the best way in your opinion?

1

votes

3

answer

42

Views

### Add number to all negative values in a 4D matrix

I have a matrix DirModel of dimension 2x2x29x1739. I want to add 360 to all negative values in this matrix, but the code I use doesn't keep up the dimensions of this matrix, it makes it into an array:
Neg=DirModel

1

votes

2

answer

45

Views

### Scalar-Vector multiplication with meshgrid (Matlab)

I have an expression such as s=aU+bV, where a and b are scalars and U and V 3-component vectors. The output s is clearly a 3-component vector. Let's assume I want to plot the first component of s find out how this varies when I change a and b.
In order to plot I have to use surf, which takes matrice...

1

votes

1

answer

27

Views

### Extract square matrices from a Nx2 matrix

I'm working on matlab 2014, I have a matrix 5124x2 and I want to extract all the 2562 square matrices.
I found that the unique solution is to use mat2Cell, but it doesn't work for me. I don't really need a cell array at the end, what I want is just all the square matrices
%example of data
A = rand(5...

1

votes

2

answer

57

Views

### Assigning color to points in the three dimensional space depending on the functional values

I was thinking of a question similar to this.
I have a function which takes as input the three values x,y,z from the R^3 and returns either 1,2,3,4. Now I wanted to plot the point in the 3D space with coordinates (x,y,z) with a color associated with the functional value at that point which can be ei...

1

votes

1

answer

46

Views

### Matlab: random integers from a range with no repetition

I want to get an array with randomized integer included in range with no repeat, I used :
randperm(15,3)
output is :
8, 10, 12
This function not use a range, I would like to random values only from 10 e.g.
Thank you !

0

votes

0

answer

20

Views

### How do I define a variable from another function?

I have a multi-fuction script that is supposed to ask the user for 4 different cars and weigh them based on ratings to give the user the best car to purchase.
What I want to do is have a prompt for every car the user inputs so the user can put in data for each variable the user decides to use. Howev...

1

votes

2

answer

34

Views

### matlab transform logical index to range

Couldn't find a quick answer for this, it seems super simple but I don't get it. I want do the following transformation (for this example using my imaginary function transform):
a=[0 0 0 1 0 0 0 0];
b=(-1:2); %rule to transform for every true value in [a], -1:2 should be true
transform(a,b) %should...

1

votes

1

answer

37

Views

### Minimizing Function with vector valued input in MATLAB

I want to minimize a function like below:
Here, n can be 5,10,50 etc. I want to use Matlab and want to use Gradient Descent and Quasi-Newton Method with BFGS update to solve this problem along with backtracking line search. I am a novice in Matlab. Can anyone help, please? I can find a solution for...

1

votes

1

answer

25

Views

### Creating an adjacency matrix from a cell array

I'm going to create an adjacency matrix from a cell array, but faced two main problems:
I don't know how to access to cell array's elements; so an adhoc method was used.
(and the most important) The code produces an error and the partial result is also a weird one!
The cell array is as the following...