Tally Instances of Negative Numbers in an Array (for loop)

57 ビュー (過去 30 日間)
Elena
Elena 2025 年 2 月 10 日 15:45
コメント済み: Walter Roberson 2025 年 2 月 10 日 19:30
Hi there.
I'm an absolute beginner. I'm trying to create a script that, given an array, will count (tally) the number of negative values, and print how many there are. This is my code
A=load('ArrayWithNegatives.mat');
Count = 0; %defines variable Count = 0
for n=(1:length(A))
if A(n) <= 0
Count = Count+1;
else Count = Count;
end
end
Count
The error I get is "Error using <= // Invalid data type. Argument must be numeric, char, or logical."
However, the code works when I manually input A=[list of numbers] instead of loading it.
Any tips or pointers to move in the right direction? I don't have to use a for loop but so far it's the only way I can think of with what I've learned.
  3 件のコメント
DGM
DGM 2025 年 2 月 10 日 16:58
FWIW
% load a specific variable from a mat file
u = load('wind.mat').u;
% count negative values using nnz and a logical test
numnegative = nnz(u<0)
numnegative = 1973
See also: nnz
Elena
Elena 2025 年 2 月 10 日 18:28
TIL! Thank you

サインインしてコメントする。

採用された回答

Matt J
Matt J 2025 年 2 月 10 日 16:11
編集済み: Matt J 2025 年 2 月 10 日 16:14
We don't know the names of the variables in the .mat file. If it contains a variable named B that you are trying to load into A, then you can do,
A=load('ArrayWithNegatives.mat').B;
  2 件のコメント
Elena
Elena 2025 年 2 月 10 日 16:42
This fixed it, thank you!
Matt J
Matt J 2025 年 2 月 10 日 16:50
You're welcome, but please Accept-click the answer to indicate so.

サインインしてコメントする。

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2025 年 2 月 10 日 16:16
A=load() returns A as a structure, not a typical matrix. If you make A an matrix, this code would work if you make the following change.
numel(A) should be used instead of length(A). Check the document to see the difference.
  3 件のコメント
Walter Roberson
Walter Roberson 2025 年 2 月 10 日 19:07
A=load() returns A as a structure, not a typical matrix.
Small modification to that:
If you load() a text file, then the result is a numeric array.
If you load() a mat file, then the result is a struct containing one field for each variable that was loaded.
Walter Roberson
Walter Roberson 2025 年 2 月 10 日 19:30
Is there a command to import data as an array rather than a structure, or to create an array from the structure?
There are two possibilities here: either you know the name of the variable being loaded, or you do not know the name of the variable being loaded.
If you know the name of the variable being loaded then use structure field referencing on the result of the load command, like
A=load('ArrayWithNegatives.mat').B;
to put the content of variable B from the file into variable A
If you do not know the name of the variable being loaded, then you need to do something like
structinfo = load('ArrayWithNegatives.mat');
varnames = fieldnames(structinfo);
A = structinfo.(varnames{1});
which would load the "first" variable from the file into A.
A = struct2array(load('ArrayWithNegatives.mat'));
Given a scalar struct containing one or more variables, struct2array() attempts to create an array by vertcat()'ing all of the variables together.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by