MATLAB Answers

How to loop column data

48 views (last 30 days)
QuanCCC
QuanCCC on 15 Jan 2019
Answered: Guillaume on 15 Jan 2019
Hi,
I have a 200*100 table data. Columns in the table are named as 'Vre1', 'Vre2',...'Vre100'. The table has 200 rows.
I want to loop the table each time use one column for my following calculation.
The first round in the loop:
AnotherData.column1 = min(AnotherData.column1, LoopTable.Vre1);
AnotherData.column2 = min(AnotherData.column2, LoopTable.Vre1);
The second round in the loop would be:
AnotherData.column1 = min(AnotherData.column1, LoopTable.Vre2);
AnotherData.column2 = min(AnotherData.column2, LoopTable.Vre2);
The last round in the loop would be:
AnotherData.column1 = min(AnotherData.column1, LoopTable.Vre100);
AnotherData.column2 = min(AnotherData.column2, LoopTable.Vre100);
And after each loop, my AnotherData should be renamed as AnotherDara1, AnotherDara2, ...AnotherDara100.
Finally, I am should calculate the statistical median of AnotherData.
Please tell me how cna I do that. Thank you.
AnotherData=rand(10,2);
AnotherData=array2table(AnotherData);
LoopTable=rand(10);
for ii=1:10;
X=LoopTable(:,ii); % there's aproblem in this line
AnotherData.AnotherData1 = min(AnotherData.AnotherData1, X);
AnotherData.AnotherData2 = min(AnotherData.AnotherData2, X);
AnotherData{ii} = AnotherData; % how to improve this line to get a statistical median of AnotherData.
end

  5 Comments

Show 2 older comments
Guillaume
Guillaume on 15 Jan 2019
If the variables in you table are just numbered variables, then there is absolutely no point in using a table. Just use a plain matrix which will be a lot faster to manipulate. Tables have convenient features at the expense of speed. And you're not using any of these features.
Numbered table variables just as numbered plain variables are always a bad idea. Do not do that! It's buggy, it's slow, it's hard to understand and you end up writing code just to manipulate the variable name instead of writing code to manipulate the variable content. Matlab has a very simple mechanism to handle numbered things. The indexing of matrices and cell arrays, so use just one variable and index that instead.
Am I correct in understanding that you want to calculate the minimum of every combination of pairs of columns taken from two different matrices (at present tables). So given a MxN matrix A and a MxP matrix B, you want a MxNxP matrix C where column C(:, n, p) = min(A(:, n), B(:, p))?
QuanCCC
QuanCCC on 15 Jan 2019
HI, yes, you are right. I then turn the tables to array and did the loop.
Now I have a new question: to storage mt result table for each loop than get the median value.
in the loop, I turen the result table to an array, reshaped the array to one column.
So how can I use the loop to store the result? At the end of the loop:
Result = table2array(Result); %result is the table of every loop run
ConResult(i)=result; % this line seems not right.How can I concatenate my array columns together?
MedianResult = median(ConResult);
Bob Nbob
Bob Nbob on 15 Jan 2019
"ConResult(i)=result; % this line seems not right.How can I concatenate my array columns together?'
You are correct that this won't work. You should be looking at something more like this:
ConResults = reshape(Results,[],1);
This will make ConResults a single column of all the data in 'Result'.
I'm not sure why you are indexing ConResult, but I assume it is to store the data for each loop. The way you have it set up now won't work. Currently you are trying to fit an entire array into a single element. This only works if the element you are trying to place the array in is a cell, which requires curly braces to define. If you are looking to add a new column to ConResults, then your indexing should be something more like (:,i).

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 15 Jan 2019
You keep mentioning loops when no loop is ever needed. I'm going to assume I was correct as to the result you want. this can be achieved simply with:
%inputs:
%A: a MxN array (if it was a table convert it to a matrix with yourtable{:, :})
%B: a MxP array
%output: C a MxNxP array where C(:,n, p) = min(A(:, n), B(:, p))
result = min(A, permute(B, [1 3 2])); %That's it! (At least in versions >= R2016b)
After that I'm not sure what you want to calculate. It looks like you want the median, but I'm not sure of what.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2018b