필터 지우기
필터 지우기

Matlab While Loop problem

조회 수: 2 (최근 30일)
Ashes
Ashes 2013년 11월 1일
답변: Mehmet Mert Aktas 2016년 5월 11일
I want to do a while loop so given array A=[2, 4, 6;9, 10, 11; -1, -3 , 12). I want generate B whose elements are the natural logarithm of the values in A if the values are greater than 1. remaining elements of B can be obtained by adding 21 to each of the corresponding elements of A

채택된 답변

Image Analyst
Image Analyst 2013년 11월 1일
A vectorized solution:
A=[2, 4, 6;9, 10, 11; -1, -3 , 12] % Define input data.
B = A + 21 % Initialize B
moreThan1 = A > 1 % Find elements greater than 1.
B(moreThan1) = log(A(moreThan1)) % Assign them to log(A).

추가 답변 (2개)

Iain
Iain 2013년 11월 1일
count = 0
while count < numel(a)
count = count + 1;
if a(count)>1
b(count) = log(a(count));
else
b(count) = log(a(count)+21);
end
end
There are other, better, faster methods.

Mehmet Mert Aktas
Mehmet Mert Aktas 2016년 5월 11일
clear clc a = [2, 4, 6; 9, 10, 11; -1, -3, -12]; b = zeros(size(a)); for i = 1: 3 for j = 1: 3 if (a(i,j) > 1) b(i,j) = log(a(i,j)); else b(i,j) = a(i,j) + 21; end end end

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by