Use of for loop for multiple columns

10 ビュー (過去 30 日間)
aa
aa 2020 年 8 月 30 日
編集済み: VBBV 2020 年 9 月 5 日
Hi everyone,
May someone help me.
The code presneted here is work well. I need to modified this for n number of columns (a=s(:,1) is the first column of S). s file consists of 1000 columns.
  4 件のコメント
aa
aa 2020 年 8 月 30 日
hi i am new .. try to apply but failed
Peter O
Peter O 2020 年 8 月 30 日
Rafael's suggestions put you on the right track, and he's given you the generalized code for k columns. If I can suggest a slight correction to the array access so that all of the columns are extracted an operated on:
clear all
clc
n_cols = 1000;
s = importdata('data.xlsx');
a=s(:,n_cols);
s_ev = sum(a(1:24,:)); % A slight correction here to extract all columns to pass to the sum function.
% This applies the zero to 0.25 substitution across columns
s_ev(s_ev == 0) = 0.25;
r = s_ev/24;
t = a(25:48,:);
% Taking a guess here that your intent is to divide the next 24 rows by the average for the first 24 rows, for every column.
% This takes advantage of some of MATLAB's broadcast operations to eliminate the for loop.
b = t - r./sqrt(r);
% Gives you the max for each column
c = max(b)

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

採用された回答

VBBV
VBBV 2020 年 9 月 5 日
編集済み: VBBV 2020 年 9 月 5 日
Try this way
% If true
% code
% end
clear all
clc
s = importdata('data.xlsx');
[R,C] = size(s)
for j = 1:C
a=s(:,j);
end
s_ev = sum(a(1:24,:));
% A slight correction here to extract all columns to pass to the sum function.
%This applies the zero to 0.25 substitution across columns
s_ev(s_ev == 0) = 0.25;
r = s_ev/24;
t = a(25:48,:);
% Taking a guess here that your intent is to divide the next 24 rows by the average for the first 24 rows, for every column.
% This takes advantage of some of MATLAB's broadcast operations to eliminate the for loop.
b = t - r./sqrt(r);
%Gives you the max for each column
c = max(b)
Make sure that you have only numeric data in .xls file of importdata function

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by