The index ranging in Matlab matrices [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I want to know what this line of code does.
ind_x = [1,3:5:size(paths,2)]
What would ind_x contain after this line? I already know that size(paths,2) means the size of second dimension of paths matrix.

3:5:size(path,2) returns a vector which starts from 3 to size(path,2) with steps 5. For example, if size(path,2) is equal to 20, the result would be:
ind_x
[1 3 8 13 18]
As you can see it counts from 3 to size(path,2) with step size 5 (3, 8, 13, ...).

Related

Problem with displaying the elements of a cell array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I have tried to create vector matrix array, to add integer elements after their arrival.
a={[1 1 1 1 1]; [3 3 3 3 3 3 3 3 3]; [4 4]; [5]};
print(a);
That code gives me this error:
You should take a look at the documentation for print. It is used to:
Print figure or save to specific file format
What you want is either disp which is used to
Display value of variable
Or fprintf which is used to:
Write data to text file (which can be the console)
Or even simpler: Just write
a % Note the absence of ';'
Upon encountering an operation without semicolon, MATLAB aromatically displays the result in the console. So this is enough to print you variable.

How the matlab code gives a strange output and How to explain it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I understand how the program works but I have a little bit of confusion. If anybody can explain, that will be great. The output is 21, 12. Does it work like 7*3=21 and 4*3=12?
mat=[7 11 3; 3:5];
[r,c]=size(mat);
for i=1:r
fprintf ('The sum is %d\n',sum(mat(i,:)))
end
mat(i,:) will give you all values in the first row of mat. In your example, this first row is [7 11 3], and the second row is [3 4 5]. The outputs you're seeing are the sums of all values in each row (7+11+3=21).

Matlab incorrect computation result [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I got a few Matlab code to play. But the answer is not correct:
x = linspace(-pi, pi, 10)
sinc = #(x) sin(x) ./ x
sinc(x) // wrong result occurs at here.
The expected result as below:
ans =
Columns 1 through 6:
3.8982e-17 2.6306e-01 5.6425e-01 8.2699e-01 9.7982e-01 9.7982e-01
Columns 7 through 10:
8.2699e-01 5.6425e-01 2.6306e-01 3.8982e-17
real result:
ans =
Columns 1 through 3
0.000000000000000 0.263064408273866 0.564253278793615
Columns 4 through 6
0.826993343132688 0.979815536051016 0.979815536051016
Columns 7 through 9
0.826993343132688 0.564253278793615 0.263064408273866
Column 10
0.000000000000000
details: My OS is arch linux,
Matlab is downloaded through official website.
matlab version is 2015b
The expected result and the real results you present are identical as far as I can see.
The only difference is the notation: normal vs scientific.
With format short you can switch to scientific notation and get identical results with identical formatting.

How can I fetch all the elements in A but not in B? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In MATLAB, how can I fetch all the element in A but not in B?
If
A = [1 2 3 4 5 6 7 8];
B = [1 2 3];
I hope the answer to be [4 5 6 7 8].
It sounds like you need setdiff().
As Oli stated you can use setdiff, however a little faster way to perform the same operation is
C = A(~ismember(A, B));
setdiff also sorts the resulting array, if you need this you have to sort C in the above statement

Store the result for for-loop [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following data: ET = [1 3 5 7 6 4], and below is my code:
for i=1:3
meanET(i)=ET(:,1+(2*i-2)); %//for i=1,extract ET column 1 data
stdET(i)=ET(:,2+(2*i-2));
totalET(i)=meanET(i)+stdET(i)
end
However, MATLAB display's an error that says that in the assignment A(I)=B, the number of elements in B and I must be the same, and therefore I modified my code to this:
for i=1:3
meanET=ET(:,1+(2*i-2));%for i=1,extract ET column 1 data
stdET=ET(:,2+(2*i-2));
totalET=meanET+stdET
end
After running the latter code, it showed meanET=6, stdET=4, and totalET=10, which means that it only stored the data for i=3 in the workspace. I want to get the result like
totalET=[4 12 10] in the workspace, corresponding to i = 1, 2, 3. How do I do that?
OR you could just go with a simple vectorized solution:
>> totalET = ET(1:2:5) + ET(2:2:6)
totalET =
4 12 10
you should just declare your target array at the beginning of your code:
meanET=zeros(size(ET,1),3);
stdET=zeros(size(ET,1),3);
for i=1:3
meanET(:,i)=ET(:,1+(2*i-2));
stdET(:,i)=ET(:,2+(2*i-2));
end
totalET=meanET+stdET