MATLAB Answers

How to get the absolute value of the a vector inside an array?

22 views (last 30 days)
Hi there, I am trying to extract the absolute value of the column vectors inside an array, but it keep giving me the message: Array indices must be positive integers or logical values., for the following code:
absvec = [];
for a = 1:270
for b =1:10
abs(H{a,b}) = absvec (a,b),
can anyone help me?


Adam Danz
Adam Danz on 10 Sep 2019
Either that's not the error message you're getting or you shared the wrong code with us.
The error message returned by your code is "Index in position 1 exceeds array bounds." (r2019a) Of course this is happeneng because absvec is empty and you're trying to index it.
do you mean
absvec (a,b) = abs(H{a,b}) % ???

Sign in to comment.

Accepted Answer

Sebastian Körner
Sebastian Körner on 10 Sep 2019
relly not sure if this is what your question is about but maybe it helps.
since you provided no data to your problem i created some example data myself.
H ={[5,-6],[5,-6];[7,-8],[7,-8];[5,-6],[5,-6];[7,-8],[7,-8]};
for i=1:4
for k=1:2
input: cell array with cevtors in each cell
output: a vector with the absolute values of each vector of the cell array


Ana Bianco
Ana Bianco on 11 Sep 2019
That is just perfect! Exactly what I wanted.
Thanks Sebastian!
Stephen Cobeldick
Stephen Cobeldick on 11 Sep 2019
Simpler and more efficient with a comma-separated list:
>> out = abs(cat(1,H{:}))
out =
5 6
7 8
5 6
7 8
5 6
7 8
5 6
7 8
Timing (1e5 iterations):
Elapsed time is 13.833 seconds. % nested loops
Elapsed time is 2.239 seconds. % comma-separated list

Sign in to comment.

More Answers (0)

Sign in to answer this question.