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

조회 수: 14 (최근 30일)
Ana Bianco
Ana Bianco 2019년 9월 10일
편집: Stephen23 2019년 9월 11일
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),
end
end
can anyone help me?
  댓글 수: 2
Adam Danz
Adam Danz 2019년 9월 10일
편집: Adam Danz 2019년 9월 10일
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}) % ???
Ana Bianco
Ana Bianco 2019년 9월 11일
I may have confused which came first, thank you!

댓글을 달려면 로그인하십시오.

채택된 답변

Sebastian Körner
Sebastian Körner 2019년 9월 10일
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]};
v=1;
for i=1:4
for k=1:2
h(:,v)=abs(H{i,k});
v=v+1;
end
end
input: cell array with cevtors in each cell
output: a vector with the absolute values of each vector of the cell array
  댓글 수: 2
Ana Bianco
Ana Bianco 2019년 9월 11일
That is just perfect! Exactly what I wanted.
Thanks Sebastian!
Stephen23
Stephen23 2019년 9월 11일
편집: Stephen23 2019년 9월 11일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by