How to call a value from cell of array in matlab? [duplicate] - matlab

This question already has an answer here:
Access to a vector in cell in Matlab
(1 answer)
Closed 5 years ago.
i have this cell array in my Matlab code
energie{n} = [Ea, Ed];
In which Ea contain 1 value and Ed contain 3 values i don't know how to call the second value of Ed .
For example if i have this
Ea =
50.9982
Ed =
1.1777 19.0690 20.2442 8.5108
and i want to call '19.0690' how can i do it ?
I tried this
ans=energie{1:n:3}
but it give me
50.9982 1.1777 19.0690 20.2442 8.5108
Also i tried
energie{n}{2}(3)
But i got this error message
"Cell contents reference from a non-cell array object."

energie{n} is a 5 element matrix. You are wanting the 3rd element of it, so energie{n}(3) will give you the element.
If you had done energie{n} = {Ea Ed} then energie{n} is a cell array containing 2 elements, where the first element is a scalar and the second element is a 4 element vector. In this case energie{n}{2}(2) would work.

Related

sorting two arrays simultaneously in Perl [duplicate]

This question already has answers here:
How do you sort parallel arrays in Perl?
(4 answers)
How to return a sorted list's index in Perl?
(2 answers)
Closed 5 years ago.
I have two arrays.
1. #store_chromosomes( This is an array of arrays)
2. #fitness_values of the same length.
#store_chromosomes = ( ("attr1", "attr2", "attr3"), ("attr4", "attr5", "attr6"), ("attr_m", "attr_n", "attr_o")); #Just an example
#fitness_values=(-0.3456, -0.6789, 0.5678); #just an example
Elements in #fitness_values corresponding to elements in #store_chromosomes, i.e., -0.3456 is generated for ("attr1", "attr2", "attr3") attribute and so on.
I need to arrange the elements in #fitness_values in descending order, i.e., (-0.6789, -0.3456, 0.5678) at the same time I need to arrange the elements of #store_chromosomes corresponding to the order of #fitness_values, i.e., (("attr4", "attr5", "attr6"), ("attr1", "attr2", "attr3"), ("attr_m", "attr_n", "attr_o") ).
For #fitness_values, I can do it by #fitness_values = sort{4b<=>$a} #fitness_values. But how to do it simultaneously for the array #store_chromosomes.

How to compare two vectors element by element? [duplicate]

This question already has answers here:
How do I compare all elements of two arrays?
(3 answers)
Closed 6 years ago.
I have two matrices: A=[1,2,3,4,5] and B=[1,2,3,4,6]. I need to compare elements of those matrix, and as a result I need to have binary matrix
Result=[1,1,1,1,0], that means if A(i)==B(i) then Result(i)=1 else Result(i)=0.
I have tried with:
if (isequal (A,B))
Result=1
else
Result=0
end
I have tried: Result=sign(A,B)
I hope that you could help me please?
A = [1,2,3,4,5]
B = [1,2,3,4,6]
Result = A == B

Remove specific integers from matrix [duplicate]

This question already has answers here:
Exclude elements from array [duplicate]
(3 answers)
Closed 6 years ago.
I have a matrix <1x1000> containing integers. It contain the value 150 a couple of times and I want to remove that value completely. Any ideas how to?
Help is much appreciated!
If you want to remove all elements that equal 150 then
M = M(M ~= 150)
If you want to remove all elements belonging to a list of undesired numbers then
list = [150, 230, 420]
M = M(~ismember(M, list))
Same but different expression
M(M==150)=[];
list = [150,230,420];
M(ismember(M,list))=[];
When you type A(index)=[], it delete A(index). For example,
A = [1,2,3];
A(2) = [];
Then
A = [1,3]

Add unknown number of elements to another cell array from a certain index on

I've data in the format of
a{1}(1,1)=1
a{1}(1,2)=3
a{1}(1,3)=0.5
a{1}(2,1)=1
a{1}(2,2)=5
a{1}(3,1)=2
a{1}(3,2)=7
...
Now I'd like to place item 1 & 2 of all rows in this cell into another cell array from an specific index on. How should I do this? for example to place column 1 & 2 of these unknown number of row in the cell array 'b' from index 5 on without repetition I used:
b{1}(1,(5:end + 1)) = unique(a{1}(:,(1:2)))
I'd like the output of cell array 'b{1}' from 5th elements becomes '1 2 3 5 7'.
However, it is false and gives the "Subscripted assignment dimension mismatch." error. I dont know where is the problem. Is my snippet right?
Any help is appreciated

Delete cells from cell array without replacing that by NULL [duplicate]

This question already has answers here:
Delete a cell array column
(3 answers)
Closed 9 years ago.
I have a cell which contains cells, I want to delete specific cell from the cell array but with 'delete' function I am getting error as "Argument must contain a string".
For example I have a cell A= { ['a'] ['b'] ['c'] [1 2 3]} now i want to delete the 3rd value of A that is A(3)='c' such that the new cell A after deleting A(3) will be as A={ ['a'] ['b'] [1 2 3]}, means there should not be any null value at the place of deleted value as A={['a'] ['b'] [] [1 2 3]} .
Please solve my problem.
Use parenthesis instead of braces:
A(1) = [];