Fail to multiply column using .*

11 views (last 30 days)
Shahid Said
Shahid Said on 30 Apr 2021
Answered: Shahid Said on 1 May 2021
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

Image Analyst
Image Analyst on 1 May 2021
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)

Scott MacKenzie
Scott MacKenzie on 30 Apr 2021
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
Shahid Said
Shahid Said on 1 May 2021
Edited: Image Analyst on 1 May 2021
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.
Image Analyst
Image Analyst on 1 May 2021
Edited: Image Analyst on 1 May 2021
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.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!