Fail to multiply column using .*

I have google and try to find the answer whole day to make below code.ONLY to make multiplication.
I already change from .* to * and try another method but doesnt work.
please teach me how.im so stress now because im very new to matlab.
thank you.
clc
clear
%Create Combination
[a,b,c,d,e]=ndgrid(1:2,1:2,1:2,1:2,1:2);
data=[a(:),b(:),c(:),d(:),e(:)];
PV={'PV1',1,2;...
'PV2',3,4};
Batt={'B1',5,6;...
'B2',7,8};
MPPT={'MPPT1',9,10;...
'MPPT2',11,12};
Inverter={'Inv1',13,14;...
'Inv2',15,16};
DieselGenerator={'DG1',17,18;...
'DG2',19,20};
ExpandData1=[PV(data(:,1),:), Batt(data(:,2),:), MPPT(data(:,3),:), Inverter(data(:,4),:), DieselGenerator(data(:,5),:)]
T1 =ExpandData1(:,3)
T2 =ExpandData1(:,3)
T3 = T1.*T2 %<<<----------------error out
Undefined operator '.*' for input arguments of type 'cell'.
Error in Entahla (line 28)
T3 = T1.*T2

 Accepted Answer

Try
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 14;
%Create Combination
[a,b,c,d,e]=ndgrid(1:2,1:2,1:2,1:2,1:2);
data=[a(:),b(:),c(:),d(:),e(:)];
PV={'PV1',1,2;...
'PV2',3,4};
Batt={'B1',5,6;...
'B2',7,8};
MPPT={'MPPT1',9,10;...
'MPPT2',11,12};
Inverter={'Inv1',13,14;...
'Inv2',15,16};
DieselGenerator={'DG1',17,18;...
'DG2',19,20};
ExpandData1=[PV(data(:,1),:), Batt(data(:,2),:), MPPT(data(:,3),:), Inverter(data(:,4),:), DieselGenerator(data(:,5),:)]
T1 =ExpandData1(:,1) % is text 'PV','PV2'
T2 =ExpandData1(:,2) % is number 1,3,1,3
T3 =ExpandData1(:,3) % is number 2,4,2
T2Array = cell2mat(T2);
T3Array = cell2mat(T3);
myTable =table(char(T1), T2Array, T3Array)
fprintf('Done running %s.m ...\n', mfilename);
You get:
myTable =
32×3 table
Var1 T2Array T3Array
____ _______ _______
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4
PV1 1 2
PV2 3 4

More Answers (2)

I'm not sure what the "big picture" is here, but if you are just trying multipy T1 times T2, then the fundamental problem is that T1 and T2 are cell arrays and the opertation you are attempting is not valid -- as stated in the error message. So, convert to numeric arrays first, then multiply:
T1 =ExpandData1(:,3)
T2 =ExpandData1(:,3)
t1Array = cell2mat(T1);
t2Array = cell2mat(T2);
T3 = t1Array .* t2Array
% T3 = T1.*T2 %<<<----------------error out
With this, your code executes without an error and generates the following output:
T3 =
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16
4
16

6 Comments

Yes.You made it ! Thank you.
But two of the column return an error.
I think this is because of the values are not number.
T1 =ExpandData1(:,3)
T2 =ExpandData1(:,3)
t1Array = cell2mat(T1);
t2Array = cell2mat(T2);
T3 = t1Array .* t2Array
let say T1 is not number.how to extract the data as other numeric data? if i use cell2mat it return error.please help again.thank you.....
Scott MacKenzie
Scott MacKenzie on 30 Apr 2021
Edited: Scott MacKenzie on 30 Apr 2021
I don't get any error when I run your code with the modification I suggested.
I'm not sure what you mean by "Let's say T1 is not a number". T1 is a cell array of numbers. It becomes a numeric array via cell2array. If you are changing T1 or T2 to hold, say, character or string arrays, that fundamentally changes the problem. In this case, multiplication doesn't make sense in the first place.
BTW, you are aware that T1 and T2 are the same, right?
clc
clear
%Create Combination
[a,b,c,d,e]=ndgrid(1:2,1:2,1:2,1:2,1:2);
data=[a(:),b(:),c(:),d(:),e(:)];
PV={'PV1',1,2;...
'PV2',3,4};
Batt={'B1',5,6;...
'B2',7,8};
MPPT={'MPPT1',9,10;...
'MPPT2',11,12};
Inverter={'Inv1',13,14;...
'Inv2',15,16};
DieselGenerator={'DG1',17,18;...
'DG2',19,20};
ExpandData1=[PV(data(:,1),:), Batt(data(:,2),:), MPPT(data(:,3),:), Inverter(data(:,4),:), DieselGenerator(data(:,5),:)]
T1 =ExpandData1(:,1)
T2 =ExpandData1(:,2)
t1Array = cell2mat(T1);
t2Array = cell2mat(T2);
Table =[T1Array,T2Array,T3Array]
Undefined function or variable 'T1Array'.
Error in Entahla (line 31)
Table =[T1Array,T2Array,T3Array]
FYI T1 value is PV1,PV2,PV3...PV20 value.not numeric data.I need this column because after calculation i can know which row to refer after get result.
i want to reorder the data in another table.because ExpandData1 i cannot make multiply or other operation.
sorry to burden you.i am newbie in matlab.
Thanks sir.
You've change the definition of T1 from T1 =ExpandData1(:,3), in your original question, to T1 =ExpandData1(:,1). This changes everything. Now, T1 contains text labels, so multiplcation doesn't make sense. Sounds like you want to do some sort of look up: pull a value from T2 or T3 based on some code that is looked up in T1. Is that right?
BTW, look carefully at your code. The error you're getting now is pretty obvious:
I'm sorry -- my mistake. I overlooked. I already append the code. Thanks sir.
T1 =ExpandData1(:,1) % is text 'PV','PV2'
T2 =ExpandData1(:,2) % is number 1,3,1,3
T3 =ExpandData1(:,3) % is number 2,4,2
t1Array = cell2mat(T1); <<_----error here because T1 is not number
t2Array = cell2mat(T2);
t3Array = cell2mat(T3);
Table =[T1Array,T2Array,T3Array]
Still error sir. I tried Google to find answer to call cell2mat method for non numeric but had no luck.
Thanks for your understanding. I'm so much appreciated. I need your help some more.
MATLAB is case sensitive, so T1Array was never defined. It is not the same as t1Array.
And you cannot convert T1 to numbers because it's character arrays. Let's say you were to convert 'PV', 'PV2', etc. to numbers. What would you expect to get???
Did you try
T1 =ExpandData1(:,1) % is text 'PV','PV2'
T2 =ExpandData1(:,2) % is number 1,3,1,3
T3 =ExpandData1(:,3) % is number 2,4,2
%t1Array = cell2mat(T1); <<_----error here because T1 is not number
t2Array = cell2mat(T2);
t3Array = cell2mat(T3);
Table =[T1Array,T2Array,T3Array]

Sign in to comment.

Shahid Said
Shahid Said on 1 May 2021
Thanks Image Analyst and Scott for your support.I love both of you.God Bless you.

Products

Release

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!