Merge two arrays w/o for-loop. Speed-up

조회 수: 4 (최근 30일)
Malte T
Malte T 2022년 10월 25일
댓글: KALYAN ACHARJYA 2022년 10월 25일
Hello Community,
I'm looking for a solution to speed up my script. I have two arrays with the same length. Both arrays must be merged.
B1 = [52, 52, 52, 52, …]
B2 = [0.437288188, 0.437288188, 0.437288188, 0.437288188, ...]
Right now I'm connecting both signals with a for loop, because I need to remove the "0." from array B2:
for k = 1:length(B1)
C(k) = str2num([num2str(B1(k)),'.',strrep(num2str(B2(k),'%.20f'),'0.','')])
end
I want that:
C = [52.437288188, 52.437288188, 52.437288188, 52.437288188, … ]
My solution is very slow. Is there a way to do this element by element without a for-loop?
Thanks in advance.
Best regards,
Malte

채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2022년 10월 25일
B1 = [52, 52, 52, 52]
B1 = 1×4
52 52 52 52
B2 = [0.437288188, 0.437288188, 0.437288188, 0.437288188]
B2 = 1×4
0.4373 0.4373 0.4373 0.4373
C=B1+B2
C = 1×4
52.4373 52.4373 52.4373 52.4373
  댓글 수: 2
Malte T
Malte T 2022년 10월 25일
Thanks a lot.
KALYAN ACHARJYA
KALYAN ACHARJYA 2022년 10월 25일
In my system with Precision set
B1 = [52, 52, 52, 52]
B2 = [0.437288188, 0.437288188, 0.437288188, 0.437288188]
C=B1+B2
#
B1 =
52 52 52 52
B2 =
0.437288188000000 0.437288188000000 0.437288188000000 0.437288188000000
C =
52.437288187999997 52.437288187999997 52.437288187999997 52.437288187999997
If the objective is only to merge two arrays, then the answer will be different.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by