MATLAB: cannot call or index into a temporary array [duplicate] - arrays

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I am trying to read data files each containing one column and 4097 rows. But my function needs total numbers of rows with even number (means 4096). So I used the MATLAB command x(2:length(x))). But my 'x' value in this command is a(:,k) and the issue is MATLAB cannot call or index into a temporary array. Any solution to this? I thank all for the support.
The code is:
for k = 1:9
with filename = sprintf('F00%d.txt',k);
a(:,k) = load(filename);
x = a(:,k)(2:length(a(:,k)));
w = tqwt(p,1,3,3);
[a1,a2,a3,a4]= deal(w{:});
m(a1,1) = mean(a1);
s(a1,1) = std(a1);
ma(a1,1) = max(a1);
mi(a1,1) = min(a1);

Unfortunately, you have to split x = a(:,k)(2:length(a(:,k))); into two lines as shown below:
temp = a(:,k);
x = temp(2:length(a(:,k)));
Please read:
Indexing of a function's return
How can I use indexing on the output of a function?

Related

Summing along an entire array in Fortran 90 [duplicate]

This question already has an answer here:
sum only on certain dimension
(1 answer)
Closed 3 years ago.
I have an array that is of size [30,3500,7000], and would like to accumulate along the first dimension so I am left with a [3500,7000] array. I have tried the following:
Implicit None
REAL,INTENT(IN) :: datastored(30,3500,7000),emptyarray(3500,7000)
REAL,INTENT(OUT) :: summed(3500,7000)
INTEGER :: i, j, r
DO i = 1,3500
DO j = 1,7000
DO r = 1,30
summed(i,j) = emptyarray(i,j) + datastored(r,i,j)
The problem with this is that, for some reason, it will not sum along the r dimension, and the summed variable will only be the last 'r' value in datastored, basically mirroring datastored(30,i,j).
Any thoughts?
summed = sum(datastored, DIM = 1)
Check this version of the Fortran standard, item 13.7.161, which defines the instrinsic sum. The Example case (iii) is exactly what you are asking for.

How to use a cell array directly returned from a function? [duplicate]

This question already has answers here:
How can I index a MATLAB array returned by a function without first assigning it to a local variable?
(9 answers)
Closed 5 years ago.
I have a path (without a file name at the end) as a string in Matlab and i want to receive the first parent directory (the directory after the last file seperator character) in it.
At the moment i'm doing it like this:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = strsplit(filePath , filesep);
>>firstParent = firstParent{end};
>>disp(firstParent);
DOF
Is there any way i can use strsplit's return value (a cell array) without assinging it to a variable first?
Something like:
>>filePath = 'D:\TRAIN_DATA\OBSTACLES\DOF';
>>firstParent = ( strsplit(filePath , filesep) ){end};
>>disp(firstParent);
DOF
Do you mean:
[~,firstParent] = fileparts ( 'D:\TRAIN_DATA\OBSTACLES\DOF' )

How to convert cell array of string numbers into numerical vector [duplicate]

This question already has answers here:
Convert cell to array in matlab
(3 answers)
Closed 6 years ago.
I am using Matlab and I have the following data:
a = {'3' '11' '7'}; % This is what I have.
My goal is to use a function to convert such cell into a vector having the following shape:
b = [3 11 7]; % What I need to get!
If I try to use the b = cell2mat(a) function the result that I get is this number: b = 3117; which is not what I want.
What function should I use to reach my goal? If possible please do not use for loops but just Matlab functions.
You're looking for str2double:
b = str2double(a);

Address each Matlab array element, one by one, independent from dimension (row vs. column) [duplicate]

This question already has an answer here:
Why, if MATLAB is column-major, do some functions output row vectors?
(1 answer)
Closed 7 years ago.
I would like to do something with each element in an array. For a row array, I can do this:
array = [1 2 3];
i = 0;
for a = array
i = i + 1;
end
fprintf('Number of iterations: %g\n', i)
Number of iterations: 3
It will output 3, so it actually accessed each array element one after another.
However if the array is a column, the same code will output just 1:
array = [1; 2; 3];
...
Number of iterations: 1
I wonder why exactly this happens and if there is a way to iterate through an array, independent from its "directional dimension" and without using for i = 1:numel(array), a = array(i).
When a for loop is initialized with an array, it iterates column by column. This may not be what you want, but that the built in behavior (see http://www.mathworks.com/help/matlab/ref/for.html).
You can force your matrix into a linear row vector, so MATLAB will iterate the elements 1 by 1 with the following:
for i = A(:)'
i % use each value of A
end
Normally, a combination of vector operations will be faster than a for loop, so only use a for loop when you can't think of the appropriate vector operation equivalent.

Storing multiple powers of matrices in matlab [duplicate]

This question already has answers here:
How to generate the first twenty powers of x?
(4 answers)
Closed 7 years ago.
I have 1000 matrices with dimensions 2x2.
What I now need to do is to get 30 consecutive powers of those matrices (A^2, A^3... ...A^30) and store them all.
I found a topic that suggested using bsxfun:
Vectorizing the creation of a matrix of successive powers
However, bsxfun does not work with cell arrays ("Error using bsxfun
Operands must be numeric arrays").
What can I do?
PS. A secondary question: once I have them, I want to plot 4 graphs (each corresponding to 1 of the elements of the 2x2 matrices) with 30 positions (x-axis) which will show confidence bands (16th and 84th percentile).
EDIT: Someone linked to a question that was similar to the one that I linked. From what I can understand, the question there is about a vector, not array of matrices.
Assuming your array A is 2-by-2-by-1000, here are two loops to make things work:
A = rand(2,2,1000);
K = 30;
%%
N = size(A,3);
APower = zeros(2,2,N,K);
APower(:,:,:,1) = A;
for i = 1:N
for k = 2:K
APower(:,:,i,k) = A(:,:,i)*APower(:,:,i,k-1);
%// Alternatively you could use:
%APower(:,:,i,k) = A(:,:,i)^k;
end
end
You need to replicate the matrix 30 times to do this using cellfun. For example,
a = repmat(A{1},1,30);% Select one of your A matrices
b = num2cell(1:30);
x = cellfun(#(a,b) a^b,a,b,'UniformOutput',false)
Since you need to run cellfun for each element of A another way is to use arrayfun as below.
a = A{1};
b = 1:30;
x = arrayfun(#(b) a^b,b,'UniformOutput',false)

Resources