Use of for loop for multiple columns

13 Ansichten (letzte 30 Tage)
aa
aa am 30 Aug. 2020
Bearbeitet: VBBV am 5 Sep. 2020
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 Kommentare
aa
aa am 30 Aug. 2020
hi i am new .. try to apply but failed
Peter O
Peter O am 30 Aug. 2020
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)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

VBBV
VBBV am 5 Sep. 2020
Bearbeitet: VBBV am 5 Sep. 2020
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

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by