1x2 Cell with each ?field? containing 2 values
Show older comments
I have a Cell called "a" which is 2x1.
so from "a" to "a{1,1}" and "a{1,2}"
Each ?field? correct me pls if this is not the right term. contains 2 values.
How can i extract the first value of both fields? If i try t = a(1,1) it gives me both values but i jsut want the first one.
Answers (1)
Ameer Hamza
on 27 Nov 2020
Edited: Ameer Hamza
on 27 Nov 2020
You have 'a' like this
a = {[1 2], [3 4]};
To access first elemets, you need to use indexing like this
a{1,1}(1)
a{1,2}(1)
If you want to extract all the first elements, then you will need to use a loop
v = zeros(size(a));
for i = 1:numel(a)
v(i) = a{1,i}(1);
end
The above for-loop can be simplified using cellfun()
v = cellfun(@(x) x(1), a);
4 Comments
Patrick Petre
on 28 Nov 2020
Ameer Hamza
on 28 Nov 2020
Can you show the actual code for your for-loop. Following shows an example of how to use it in for-loop
a = {[1 2], [3 4]};
v = zeros(size(a));
for i = 1:numel(a)
v(i) = a{1,i}(1);
end
disp(v)
Patrick Petre
on 28 Nov 2020
Edited: Patrick Petre
on 28 Nov 2020
Ameer Hamza
on 29 Nov 2020
Can you attach your actual code with all the relevant variables?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!