Converting a matrix to a vector [closed] - matlab

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
I have a matrix that is 3*3. I want to convert it to a 6*1 vector in Matlab.
When I use reshape, it has an error:
To RESHAPE the number of elements must not change.
So I can't use reshape.
Do you have any suggestion that help me to convert this matrix to a 6*1 vector?

Just to illustrate Mohsen's comment, it sounds like you're asking to do something like this, which would involve losing part of your original matrix.
>> A = [1 4 7;
2 5 8;
3 6 9];
>> B = A(1:6)
B =
1 2 3 4 5 6
>> B = A(4:9)
B =
4 5 6 7 8 9
>> B = A([1:3 7:9])
B =
1 2 3 7 8 9

Related

What does X(1:n, 1) mean in Matlab? [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 2 years ago.
Improve this question
In a sample code I have X(1:n, 1)
I don't understand how it works
n=3 in a sample
Is it a matrix? but isn't () used for indexing so how it's a double index?
I imagine that your X is a two-dimmensional matrix. Generalizing, it can be said that to access the elements of a matrix is done with: X(n,m).
The common case is to get one single number, in that case n and m are integer numbers. But you can also pass vectors to n and m positions and that way extract a submatrix from the original one.
As an example:
X = [1 2 3; 4 5 6; 7 8 9]
X(1:3,1) = [1; 4; 7]

extract values from structure in matlab [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 a question please if we have structure with 3 dimension and each field of them has 7 values how can extract each value from each field of this structure separately.
Just use indexing:
>> s(1,1,1).data = [1 2 3 4];
>> s(1,1,2).data = [10 20 30 40 50]; %// example struct
>> s(1,1,2).data(3)
ans =
30
>> s(1,1,2).data(2:4)
ans =
20 30 40
Also, it's better not to use struct as a variable name, because struct is a built-in function.

How to do element-wise calculation on all element pairs [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
a=[4 2 5 9]
The I have to write a code using for loop to get the following vector:
[4-2 4-5 4-9 2-5 2-9 5-9]
How can I do this in MATLAB?
You don't need a for loop:
result = tril(bsxfun(#minus,a(:).',a(:)),-1);
ind = tril(reshape(1:numel(result),size(result)),-1);
ind = ind(ind>0);
result = result(ind).'
a = [ 4 2 5 9]
a =
4 2 5 9
s = combnk(a, 2)
s =
5 9
2 9
2 5
4 9
4 5
4 2

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