MATLAB Answers

How to output a vector/array from a created function

30 ビュー (過去 30 日間)
Omar Sinno
Omar Sinno 2019 年 6 月 19 日
編集済み: Stephen Cobeldick 2019 年 6 月 21 日
In short, I have an array that has numbers from 0 to 100 using a step of 0.01, I created a function that is supposed to output another array containing 1's and -2's. While looping on the indices of my first array if the number is smaller than 40, I insert 1 in my output array, otherwise I insert a -2. I don't know what's the problem in my code:
function [outputVector] = myOutput(inputVector)
outputVector = [];
for i=1 : size(inputVector)
if inputVector(i) < 40
outputVector = [outputVector,1] ;
else
outputVector = [outputVector,-2];
end
end
end

  0 件のコメント

サインイン to comment.

採用された回答

Shwetank Shrey
Shwetank Shrey 2019 年 6 月 20 日
編集済み: Shwetank Shrey 2019 年 6 月 21 日
The vector that you would be creating using 0 : 0.01 : 100 would have a size of 1x10001.
>> size(0 : 0.01 : 100)
ans =
1 10001
Your for loop on the size returns the first dimension whereas you require the second dimension.
Changing
for i=1 : size(inputVector)
to
for i=1 : size(inputVector, 2)
should work for you.

  2 件のコメント

Stephen Cobeldick
Stephen Cobeldick 2019 年 6 月 20 日
size([0:0.01:100])
% ^ ^ the square brackets are superfluous.
Shwetank Shrey
Shwetank Shrey 2019 年 6 月 21 日
Thanks. I wasn't aware of this and will edit the answer accordingly.

サインイン to comment.

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 6 月 19 日
編集済み: James Tursa 2019 年 6 月 19 日
Don't use size(inputVector) for your loop indexing limits since this is a vector. Use numel(inputVector) instead.
Also, you shouldn't be increasing the size of outputVector iteratively inside your loop. Each time you do that, the memory for the variable has to get copied to new memory. This can hurt the performance in a massive way. Instead, pre-allocate and use indexing. E.g., instead of this:
outputVector = [];
:
outputVector = [outputVector,1] ;
do this:
outputVector = zeros(size(inputVector));
:
outputVector(i) = 1;
There are also ways to calculate your result without using a loop.

  3 件のコメント

Omar Sinno
Omar Sinno 2019 年 6 月 20 日
My problem is solved but I'd love to know how to do it without a loop.
Stephen Cobeldick
Stephen Cobeldick 2019 年 6 月 20 日
"I'd love to know how to do it without a loop."
Not only are these simpler than your code, they will also be much more efficient.
Method one: basic arithmetic:
>> V = randi([1,100],1,17)
V =
93 45 83 48 1 41 14 93 43 8 41 64 46 10 21 48 65
>> 3*(V<40)-2
ans =
-2 -2 -2 -2 1 -2 1 -2 -2 1 -2 -2 -2 1 1 -2 -2
Method two: basic indexing:
>> X = [-2,1];
>> X(1+(V<40))
ans =
-2 -2 -2 -2 1 -2 1 -2 -2 1 -2 -2 -2 1 1 -2 -2
Steven Lord
Steven Lord 2019 年 6 月 20 日
A different way that doesn't require logical arithmetic but just logical indexing:
V = randi([1, 100], 1, 17);
result = ones(size(V));
result(~(V < 40)) = -2;
% or
result = repmat(-2, size(V));
result(V < 40) = 1;

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by